0
from dropbox import Dropbox
from dropbox import DropboxOAuth2FlowNoRedirect

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 = raw_input("Enter the authorization code here: ").strip()

try:
    access_token, user_id = auth_flow.finish(auth_code)
except Exception, e:
    print('Error: %s' % (e,))
    return

dbx = Dropbox(access_token)

I got this code from http://dropbox-sdk-python.readthedocs.io/en/master/moduledoc.html#module-dropbox.oauth

Basically what I want to is, test some simple commands. In the beginning of my code I define

APP_KEY = 'mykey'
APP_SECRET = 'mysecret'

Whenever I run the code, give access to my app and put in the authorization code it gives out an error.

NameError: name 'access_token' is not defined

I am using python2.7. There is something wrong with the try/exception part, but I can not figure out what.

Can anyone help?

4
  • 2
    Perhaps they forgot to add a function there, otherwise return statement there is a syntax error. Commented Dec 8, 2016 at 13:20
  • are you seeing 'Error: <something>' when you run it? have you tried printing out the results of the auth_flow.finish() call within the try/except? what have you tried? Commented Dec 8, 2016 at 13:22
  • return is used to exit function, not program/script. To exit script use exit() or use access_token = None before try/except and if access_token: dbx = Dropbox(access_token)
    – furas
    Commented Dec 8, 2016 at 14:46
  • [Cross-linking for reference: dropboxforum.com/t5/API-support/… ]
    – Greg
    Commented Dec 8, 2016 at 18:14

1 Answer 1

0

I tried you code and it seems something was changed in code but not in documentation.

Now auth_flow.finish(auth_code) returns object OAuth2FlowNoRedirectResult(), not tuple with access_token, user_id, and this object has properties obj.access_token, obj.user_id, obj.account_id

from dropbox import Dropbox
from dropbox import DropboxOAuth2FlowNoRedirect
import webbrowser

APP_KEY = 'xxx'
APP_SECRET = 'xxx'

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."

webbrowser.open(authorize_url) # open page in browser

auth_code = raw_input("Enter the authorization code here: ").strip()

obj = None

try:
    obj = auth_flow.finish(auth_code)
    #print obj
    #print type(obj)
    #print dir(obj)
    print obj.access_token, obj.account_id, obj.user_id
except Exception, e:
    print 'Error:', e
    #exit()

if obj and obj.access_token:
    dbx = Dropbox(obj.access_token)
    print 'OK'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.