Home > Mobile >  How can write curl command to upload file into dropbox with refresh code?
How can write curl command to upload file into dropbox with refresh code?

Time:10-14

Upload file with access token in curl command:

access_token="xxxxxxx"
remote_path="/dir_dropbox/fname.txt"
local_path="/tmp/fname.txt"
curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer $access_token" \
    --header "Dropbox-API-Arg: {\"path\": \"$remote_path\"}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @$local_path

Now i create a refresh code ,How can write curl command to upload file into dropbox with it?

refresh_token="xxxxxxx"
remote_path="/dir_dropbox/fname.txt"
local_path="/tmp/fname.txt"
curl -X POST https://content.dropboxapi.com/2/files/upload \
    --header "Authorization: Bearer $refresh_token" \
    --header "Dropbox-API-Arg: {\"path\": \"$remote_path\"}" \
    --header "Content-Type: application/octet-stream" \
    --data-binary @$local_path

It can't work!

CodePudding user response:

Refresh tokens are not access tokens, and cannot be used like access tokens. That is, you can't use a refresh token as a Bearer token in the Authorization header.

Instead, you can use a refresh token to perform a different call first to retrieve a new access token. The call to use a refresh token to retrieve an access token would look like this: (taken from the documentation for the Dropbox /oauth2/token endpoint)

curl https://api.dropbox.com/oauth2/token \
    -d grant_type=refresh_token \
    -d refresh_token=<REFRESH_TOKEN> \
    -u <APP_KEY>:<APP_SECRET>

Once you get the access token from the response to that call, you can then proceed to use it to call the API, e.g., /2/files/upload, normally.

You can find more information in the following resources:

  • Related