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?

Memo44
  • 21
  • 2
  • 2
    Perhaps they forgot to add a function there, otherwise `return` statement there is a syntax error. – Ashwini Chaudhary Dec 08 '16 at 13:20
  • are you seeing 'Error: ' 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? – matias elgart Dec 08 '16 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 Dec 08 '16 at 14:46
  • [Cross-linking for reference: https://www.dropboxforum.com/t5/API-support/I-get-a-NameError-when-I-use-the-noredirect-code-out-of-the/m-p/196994#M9058 ] – Greg Dec 08 '16 at 18:14

1 Answers1

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'
furas
  • 134,197
  • 12
  • 106
  • 148