7

I want to avoid having to authorise this script over and over. In other words, when I launch the script from the terminal, it gives me a link I have to open in a browser then click on the 'Allow' button in the browser then go back to the terminal...I guess there's a way to save the authentication details but how?

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website

APP_KEY = 'xxxxxxxxxxx'
APP_SECRET = 'yyyyyyyyyyyy'
ACCESS_TYPE = 'dropbox'


sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )

request_token = sess.obtain_request_token()

# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)

client = client.DropboxClient(sess)
#stored_creds = open(CONF_DIR + self.TOKEN_FILE).read()
print "linked account:", client.account_info()

f = open('t.txt')
response = client.put_file('/uploaded_with_python.txt', f)
print "uploaded:", response

folder_metadata = client.metadata('/')
print "metadata:", folder_metadata

f, metadata = client.get_file_and_metadata('/uploaded_with_python',rev='362e2029684fe')
out = open('/uploaded_with_python.txt', 'w')
out.write(f)
print(metadata)

--------------------------------------------------------------------------------------------EDIT

I modified the script and it created the script however I still have problems reading the token file

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website

APP_KEY = 'i4ffahjltei1bnu'
APP_SECRET = 'cjullao1iiymrse'
ACCESS_TYPE = 'dropbox'

#acces token file
token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()

sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )

request_token = sess.obtain_request_token()

# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
#save token file
TOKENS = 'dropbox_token.txt'
token_file = open(TOKENS,'w')
token_file.write("%s|%s" % (access_token.key,access_token.secret) )
token_file.close()

client = client.DropboxClient(sess)

print "linked account:", client.account_info()

f = open('t.txt')
response = client.put_file('/uploaded_with_python.txt', f)
print "uploaded:", response

folder_metadata = client.metadata('/')
print "metadata:", folder_metadata

f, metadata = client.get_file_and_metadata('/uploaded_with_python',rev='362e2029684fe')
out = open('/uploaded_with_python.txt', 'w')
out.write(f)
print(metadata)

I get this error:

Traceback (most recent call last):
  File "dropb.py", line 14, in <module>
    token_file = open(TOKENS)
NameError: name 'TOKENS' is not defined
2
  • the error "name 'TOKENS' is not defined" says it all: that is because in your edit code, you've written the definition "TOKENS = 'dropbox_token.txt'" a few lines after you've used it first, which is the line "token_file = open(TOKENS)"... Just move the definition line earlier in your code, before the first usage line appears. Commented Mar 12, 2014 at 11:56
  • Do you really want to share the APPKEY and the APPTOKEN with the internet? Commented Aug 19, 2014 at 20:11

1 Answer 1

20

You can write the access_token to a file:

TOKENS = 'dropbox_token.txt'
token_file = open(TOKENS,'w')
token_file.write("%s|%s" % (access_token.key,access_token.secret) )
token_file.close()

If you do that once, then afterwords you can use that token:

token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()

sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )
sess.set_token(token_key,token_secret)
client = client.DropboxClient(sess)
Sign up to request clarification or add additional context in comments.

1 Comment

The token file is saved however I got this error: Traceback (most recent call last): File "dropb.py", line 11, in <module> token_file = open(TOKENS) NameError: name 'TOKENS' is not defined

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.