cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Musicians, convert your MuseScore files to PDF to play music on the go! Learn more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Refresh Token for Python Project

Refresh Token for Python Project

masonml
New member | Level 2

Hi all,

 

I'm unfamiliar with APIs and this is my first project of this type so any help is appreciated. I've written a python code that uploads an image from a camera to my Dropbox app. The code works fine, but I'm struggling with the OAuth portion as I need a permanent access code since I will be away from the code/computer while the code is running. 

 

I ran the code below with my app keys:


import dropbox
from dropbox import DropboxOAuth2FlowNoRedirect

'''
This example walks through a basic oauth flow using the existing long-lived token type
Populate your app key and app secret in order to run this locally
'''
APP_KEY = ""
APP_SECRET = ""

auth_flow = DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)

authorize_url = auth_flow.start()
print("1. Go to: " + authorize_url)
print("2. Click \"Allow\" (you might have to log in first).")
print("3. Copy the authorization code.")
auth_code = input("Enter the authorization code here: ").strip()

try:
oauth_result = auth_flow.finish(auth_code)
except Exception as e:
print('Error: %s' % (e,))
exit(1)

with dropbox.Dropbox(oauth2_access_token=oauth_result.access_token) as dbx:
dbx.users_get_current_account()
print("Successfully set up client!")

The client was successfully set up after I entered the key from my browser, but no refresh token appeared on Dropbox. I attempted again on a different computer (I was testing the code on my laptop, which is not the computer that will be running the code, and received this error:

 

Error: HTTPSConnectionPool(host='api.dropboxapi.com', port=443): Max retries exceeded with url: /oauth2/token (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1129)')))

 

Is this because I already set up the client on my laptop, or a separate error altogether? How can I fix it?

1 Reply 1

Greg-DB
Dropbox Staff

Dropbox is no longer offering the option for creating new long-lived access tokens. Dropbox is now issuing short-lived access tokens (and optional refresh tokens) instead of long-lived access tokens. You can find more information on this migration here.

Apps can still get long-term access by requesting "offline" access though, in which case the app receives a "refresh token" that can be used to retrieve new short-lived access tokens as needed, without further manual user intervention. Refresh tokens do not expire automatically and can be used repeatedly. You can find more information in the OAuth Guide and authorization documentation. There's a basic outline of processing this flow in this blog post which may serve as a useful example.
 
Also, note that you do not need to implement the refresh process yourself. The Dropbox Python SDK will handle that logic for you as long as you pass the refresh token, app key, and if needed (when not using PKCE), app secret to the Dropbox client. The code you used does not request a refresh token. Instead, for the official Dropbox Python SDK, you can find examples of how this works with refresh tokens at the following links:

 

As for the SSL error you mentioned, that seems to indicate an issue establishing a secure connection to the Dropbox API servers from that computer. Check for anything on that computer, such as a VPN, proxy, firewall, or other security software that may be interfering with your network connection.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?