Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get and use a Refresh Token for the Dropbox API (Python 3.x)

As the title says, I am trying to generate a refresh token, and then I would like to use the refresh token to get short lived Access tokens.

There is a problem though, in that I'm not smart enough to understand the docs on the dropbox site, and all the other information I've found hasn't worked for me (A, B, C) or is in a language I don't understand.

I have tried out all three examples from the github page, as well as user code from other questions on this site.

I haven't got anything to work.

The most I got was

Error: 400 Client Error: Bad Request for url: api.dropboxapi.com/oauth2/token

and

dropbox.rest.RESTSocketError: Error connecting to "api.dropbox.com": [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)

:(

like image 955
Timi Avatar asked Dec 10 '25 10:12

Timi


2 Answers

Here is how I did it. I'll try to keep it simple and precise

Replace <APP_KEY> with your dropbox app key in the below Authorization URL

https://www.dropbox.com/oauth2/authorize?client_id=<APP_KEY>&token_access_type=offline&response_type=code

Complete the code flow on the Authorization URL. You will receive an AUTHORIZATION_CODE at the end.

Go to Postman and create a new POST request with below configuration

  • Request URL- https://api.dropboxapi.com/oauth2/token
  • Authorization -> Type = Basic Auth -> Username = <APP_KEY> , Password = <APP_SECRET> (Refer this answer for cURL -u option)
  • Body -> Select "x-www-form-urlencoded"
Key Value
code <AUTHORIZATION_CODE>
grant_type authorization_code

After you send the request, you will receive JSON payload containing refresh_token.

{
    "access_token": "sl.****************",
    "token_type": "bearer",
    "expires_in": 14400,
    "refresh_token": "*********************",
    "scope": <SCOPES>,
    "uid": "**********",
    "account_id": "***********************"
}

In your python application,

import dropbox

dbx = dropbox.Dropbox(
            app_key = <APP_KEY>,
            app_secret = <APP_SECRET>,
            oauth2_refresh_token = <REFRESH_TOKEN>
        )

Hope this works for you too!

like image 71
Sparrow Avatar answered Dec 12 '25 01:12

Sparrow


All methods above work, just want to post a pure python solution, which itself draws reference from the answers above.

  1. Find the APP key and App secret from the App Console.
  2. Run the following snippet (replace APP_KEY with the value obtained from last step) and complete the process in the browser to obtain Access Code Generated.
import webbrowser

APP_KEY = '<APP_KEY>'
url = f'https://www.dropbox.com/oauth2/authorize?client_id={APP_KEY}&' \
      f'response_type=code&token_access_type=offline'

webbrowser.open(url)
  1. Replace all APP_KEY, APP_SECRET, and ACCESS_CODE_GENERATED with the actual values in the following snippet. Run the snippet.
import base64
import requests
import json

APP_KEY = '<APP_KEY>'
APP_SECRET = '<APP_SECRET>'
ACCESS_CODE_GENERATED = '<ACCESS_CODE_GENERATED>'

BASIC_AUTH = base64.b64encode(f'{APP_KEY}:{APP_SECRET}'.encode())

headers = {
    'Authorization': f"Basic {BASIC_AUTH}",
    'Content-Type': 'application/x-www-form-urlencoded',
}

data = f'code={ACCESS_CODE_GENERATED}&grant_type=authorization_code'

response = requests.post('https://api.dropboxapi.com/oauth2/token',
                         data=data,
                         auth=(APP_KEY, APP_SECRET))
print(json.dumps(json.loads(response.text), indent=2))
like image 22
chjch Avatar answered Dec 12 '25 00:12

chjch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!