Unable to open downloaded file from API

Hi,

I could download files based on id through the API but after download the file doesnt open.
It shows either corrupted or sent as an attachment but same file after downloading from POSTMAN response works.

I am using Pentaho to hit the APIs and javascript below to save file.
Please suggest.

var dir=‘<download_path>’
var fileURL = url;

var url = java.net.URL(fileURL);

var new_filename=mail_message_id+‘‘+id+’’+file_name
//var new_filename=file_name

var httpConn = url.openConnection();

// opens input stream from the HTTP connection
var inputStream = httpConn.getInputStream();
var saveFilePath = dir+new_filename;

var bis = java.io.BufferedInputStream(inputStream);
var bos = java.io.BufferedOutputStream(java.io.FileOutputStream(java.io.File(saveFilePath)));
var inByte;
while((inByte = bis.read()) != -1) {
bos.write(inByte);
}
bis.close();
bos.close();

Hi @sss25
Welcome to the community :wave: and thanks for sharing the code snippet. From which endpoint are you obtaining the file URL? Could you please share further details?

Couple of initial thoughts:

  • When you mention that the file doesn’t open, could it be due to the lack of file extension?
  • Were you able to inspect the file contents of the one downloaded via Postman vs. via the code snippet? You can test it with a small file with text content for starters.

Hi Hem,

The file url and other details I am getting is from
https://api.pipedrive.com/v1/files?start=1&include_deleted_files=1&sort=0&api_token=
https://app.pipedrive.com/api/v1/files/2/download?api_token=

  1. The file is getting saved with extension also e.g. png,pdf
  2. I could not actually check content of file.
  3. Also if you can guide me about issue that some files get 401 unauthorised access but not for some.

P.S. I am using Pentaho tool E2E activity.

Thanks @sss25 for the details

You seem to be using the right endpoints. Let’s probably take a closer look at these files.

Can you share the same file before and after the download? I have DM’d you privately so that you can share it with me directly.

Do you happen to use google drive or are they files directly added to Pipedrive?

Hi @Hem ,

I was able to open the files. It was not issue with files but the approach of pentaho. Using javascript and saving bytestream was not working, instead parsing it as HTTP response worked. I have DM’d you the snippet.

Now I am having issue at download itself.
Even after using correct API token it gives below response. Is there any way we can download those files.

"success": false,
"error": "unauthorized access",
"errorCode": 401,
"error_info": "Please check developers.pipedrive.com"

Thanks a lot,
Sagar Swami

Hi Sagar, Thanks for sharing the details on what worked. Can you share details on how you access Pipedrive API via Pentaho? I am not familiar with the interface but I can probably help you with the configs :slight_smile: Feel free to share the screenshots and logs. Make sure the API token is blurred / hidden

Hello guys,

After researching so much I get to know that, if we want to download the files from pipedrive we have to get the first files data from deal/files/ api.

Then we will get the url and name, which we have to pass to file_get_contents.

$fileId = 538697;

$downloadurl = “https://” . $company_domain . “.pipedrive.com/v1/files/$fileId /download?api_token=$apiToken”;
$file_name = ‘pipedrive_sample_data.xlsx’;

//save the file by using base name
if(file_put_contents( $file_name,file_get_contents($downloadurl))){
echo “File downloaded successfully!”;
}else{
echo “File downloading failed!”;
}