Hi, I’m currently integrating CMS Files API to my project to be able to upload files from my project to the HubSpot File Manager. I followed and used the sample PHP script on https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file but I kept getting Error 400 - Bad Request response from the server. I’m using Oauth2.0 for authentication.
function upload_file($file_attachment = null) {
if (!$file_attachment || !$this->access_token) return;
$this->url = 'http://api.hubapi.com/filemanager/api/v3/files/upload';
$this->headers[] = 'content-type: multipart/form-data';
$this->headers[] = sprintf('Authorization: Bearer %s', $this->access_token);
$upload_file = new CURLFile($file_attachment['file_attachment']['tmp_name'], $file_attachment['file_attachment']['type'], $file_attachment['file_attachment']['name']);
$file_options = array(
"access" => "PUBLIC_INDEXABLE",
"ttl" => "P3M",
"overwrite" => false,
"duplicateValidationStrategy" => "NONE",
"duplicateValidationScope" => "ENTIRE_PORTAL"
);
$post_data = array(
"file" => $upload_file,
"fileName" => $file_attachment->title,
"options" => json_encode($file_options),
"folderPath" => "/docs",
);
$process = $this->process($post_data, 'POST', TRUE);
return $process;
}
function process($data = null, $action = 'POST', $return_response = FALSE) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($action !== 'GET') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POST, 1);
}
if($action == 'DELETE') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
if (count($this->headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
if ($return_response) return $result;
if ($http_code == 200) return TRUE;
return;
}
I also checked other posts like https://community.hubspot.com/t5/APIs-Integrations/Not-able-to-upload-file-through-file-API/td-p/301404 - which uses the old v2 file upload endpoint, tried applying it to my code, but still returning the same error.
I tested other endpoints like Delete a File, Create a Folder and it works properly. Upload a file is the only endpoint that returns Error 400. Any help would be appreciated.