How to share files in dropbox api using node.js - node.js

I want to share uploaded files in dropbox with another member.
add_member={
method: "POST",
url:'https://api.dropboxapi.com/2/sharing/add_file_member',
headers:{
"content-Type":"application/json",
"Authorization": "Bearer " + access_token,
"Data":"{\"file\": \"id:3kmLmQFnf1AAAAAAAAAAAw\",\"members\": [{\".tag\": \"email\",\"email\": \"jyotijagtap2209#gmail.com\"}],\"custom_message\": \"This is a custom message about ACME.doc\",\"quiet\": false,\"access_level\": \"viewer\",\"add_message_as_comment\": false}"
},
body:content
}
request(add_member,function(err,res,body){
console.log("link shared", body);
})
This is my code. I have also read documentation for Dropbox API but I can't get what is id given to this file and I didn't get any error. What I am missing?

The file parameter you supply to /2/sharing/add_file_member should be the id for the file for which you want to add a file member.
You can get the id for a file from the Metadata for the file, such as is returned by /2/files/get_metadata or /2/files/list_folder[/continue], for instance.
Also, note that /2/sharing/add_file_member uses the RPC request/response style, so your parameters should be sent as JSON in the request body, not a header.
In any case, make sure you check the resulting response status code and body to see if the call succeeded or failed, and to retrieve any returned result or error information.

Related

Scrapy - is it possible to extract Payload Request from Response

is it possible to extract and set as variable the "Payload Request" which has been pushed in order to receive particular response?
You can access the request object in the callback function by response.request.
This object is the request object itself, so it contains everything you passed in the request. It doesn't have a "payload" attribute though.
The equivalent should be response.request.body, assuming you had a body in the request. Everything else is still there, headers, cookies, meta, method, etc
More on the params of request here.

Microsoft Graph API PUT OneDrive/SharePoint

Trying to post a file to a subfolder of the Shared Documents folder. I thought I had the correct syntax down, but I keep getting StatusCode 400 Bad Request.
https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives/b!tDVUScNgt0mPbh0mKhIK5WefrQ-oNQtMiS4RMIQFjAqJk9Tt237bQYC9yEkyNOr6/items/01JDP7KXJ7ZSCYHUJC7BFJW2X6BTR4Z4JH:/filename.xlsx:/content
where "filename" is the actual filename.
I know a GET to the following lists the subfolder:
https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives/b!tDVUScNgt0mPbh0mKhIK5WefrQ-oNQtMiS4RMIQFjAqJk9Tt237bQYC9yEkyNOr6/items/01JDP7KXJ7ZSCYHUJC7BFJW2X6BTR4Z4JH
Request is going out as:
{Method: PUT, RequestUri: 'https://graph.microsoft.com/v1.0/sites/xxxxxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives/b!tDVUScNgt0mPbh0mKhIK5WefrQ-oNQtMiS4RMIQFjAqJk9Tt237bQYC9yEkyNOr6/items/01JDP7KXJ7ZSCYHUJC7BFJW2X6BTR4Z4JH:/', Version: 2.0, Content: <null>, Headers:
{
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJub...BXS_cSg1CcZHj5Q
}}
Seems like it is dropping part of the request to me.
First, https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com,495435b4-60c3-49b7-8f6e-1d262a120ae5,0fad9f67-35a8-4c0b-892e-113084058c0a/drives and https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com/drives will return the same results, we prefer the second one.
I have never sucessful run the following API:
/drives/{drive-id}/items/{parent-id}:/{filename}:/content
But based on my test, the following API works well:
/v1.0/me/drive/root:/Test/Test1.txt:/content
or
/v1.0/me/drives/driveid/root:/Test/Test1.txt:/content

What does "content-type" mean in headers of python requests library and if the value is text/html;charset=UTF-8?

I want to do some operations with response from python requests library. After I use below function;
response = requests.get(f'{AUTHORIZE_URL}?client_id={CLIENT_ID}&response_type=code&state={STATE}&redirect_uri={REDIRECT_URI}')
I need to get an URL something like this in return;
http://127.0.0.1:8000/products/auth/?state=2b33fdd45jbevd6nam&code=MGY1MTMyNWY0YjQ0MzEwNmMxMjY2ZjcwMWE2MWY5ZDE5MzJlMjA1YjdkNWExNGRhYjIzOGI5NzQ5OWZkNTA5NA
While doing it, it will be easier to use JSON in order to get state and code values from URL but I cannot use it because I think the content type does not allow this.
See this for Content-Type explanation: Content-Type
In short the "content-type" in the headers of response got by using requests.get tells you what kind of the content server did send, in your case you'we got a response in the form of the HTML (like .html document) and you can read that response with response.text, if the "content-type" is "application/json" then you can read it as JSON like this response.json().
I see that you use some local server, your local server should send in headers "Content-Type": "application/json" and then you should be able to read JSON from response like this (you need to send JSON not hmtl or text from server):
targetURL = 'http://127.0.0.1:8000/products/auth/?state=2b33fdd45jbevd6nam&code=MGY1MTMyNWY0YjQ0MzEwNmMxMjY2ZjcwMWE2MWY5ZDE5MzJlMjA1YjdkNWExNGRhYjIzOGI5NzQ5OWZkNTA5NA'
response.get(targetURL).json()

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' }
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...
});
It seems your request has an error.
please try the below method and get raw file content from its response.
podio.request('get', '/file/{file_id}').then(console.log);
FYI, we couldn't get the files by filtering the items. we need to request /item/{item_id} individually to get the files property as you said.

Unable to upload file to Box using Node

I'm having a really tough time getting my files to upload to box using Node.js.
Every single time I attempt to, I get the following error:
Error: cannot POST /api/2.0/files/content (400)
Here is the relevant code. I've already double checked that this.options.auth contains the required tokens, etc. The parent_id folder is the root folder, so '0'. The filepath is a stream, which is totally fine.
request.post('https://upload.box.com/api/2.0/files/content')
.set('Authorization', this.options.auth)
.field('parent_id', folder)
.attach('filename', filepath)
.end(function (res) {
if (res.error) {
return callback('Error: '+res.error.message);
}
callback(null, res.body);
});
Any ideas?
HTTP status code 400 is used for a bad request. One thing to check is that the parameters you are supplying are all valid and that you haven't forgotten any required parameters. Looking at the Box API getting-started doc, it appears that what you are calling parent_id should be just parent. If it still doesn't work, check for other similar issues too, of course.

Resources