Pandaiolo Pandaiolo - 4 months ago 19
Node.js Question

Podio file attached to item cannot be downloaded

I have an issue trying to download files attached to Podio items:

podio.request('get', '/file/{file_id}/raw').then(console.log);


The above program displays:

{}


This is a JSON stringified empty object (instead of raw file content).

Details:


  • The above file can be accessed with its URL when logged in

  • The above code is run after proper authentication

  • It actually works when using a
    file_id
    from an image field of the item, but not from a file attachment (pdf files in my case).

  • When using API endpoint
    /item/app/{app_id}/filter
    to get a list of items, the property
    file_count
    is set, but not
    files
    . I have to request
    /item/{item_id}
    individually to get the
    files
    property included in the response, not sure why.



Question: Do you know what is the issue, and how I can download raw attached files?

EDIT: aditionnal info

If I request a single file metadata using the folowing command:

podio.request('get', '/file/1234').then(console.log);


I get a file JSON object which includes many fields, but not the file content :

{
...
link: 'https://files.podio.com/1234',
file_id: 1234,
...
}


As stated in my comment to @stengaard, if I try to request the API for the above link, here is the response :

{ [PodioNotFoundError: [object Object]]
message:
{ error_parameters: {},
error_detail: null,
error_propagate: false,
request:
{ url: 'http://api.podio.com/1234',
query_string: '',
method: 'GET' },
error_description: 'No matching operation could be found. The path \'/1234\' was not found..',
error: 'not_found' },
status: 404,
url: 'https://api.podio.com:443/1234',
name: 'PodioNotFoundError' }

Answer

To use the GET /file/{file_id}/raw endpoint you need an API key with elevated trust levels.

Instead use GET /file/{file_id} endpoint. That contains a link attribute (a URL) you should follow to get the file content.

The link attribute will look like: https://files.podio.com/{file_id}. To fetch the file do https://files.podio.com/{file_id}?oauth_token={oauth_token}. Where the OAuth token is the same as the one used to GET /file/{file_id}. If you know the file ID (e.g. from a GET /item/{item_id} you can skip the GET /file/{file_id} and contact files.podio.com directly. (Note: You can also set the Authorization: OAuth2 {oauth_token} header in your HTTP request if you don't like passing the auth token in a URL paramter.)

For an example on how to use it see https://github.com/podio/podio-js/blob/master/lib/general.js#L11

Typically in the JS client, if you use podio as your Podio API object, the OAuth token would be located there:

podio.authObject.accessToken

So to get the raw content of the file in nodejs:

var url = 'https://files.podio.com/'+file_id+'?oauth_token='+podio.authObject.accessToken;
request(url, function (err, fileContent) {
    // use fileContent here, write to a file, etc...
});