2012-05-11 50 views
7

我想避免一次又一次地授权这个脚本。换句话说,当我从终端启动脚本时,它给了我一个链接,我必须在浏览器中打开,然后单击浏览器中的“允许”按钮,然后返回到终端...我想有一种方法如何保存身份验证的详细信息?python dropbox api - 保存令牌文件?

# 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) 

------------------------------------------- - - - - - - - - - - - - - - - - - - - - - - - - -编辑

我修改剧本,它创建的脚本,但是我仍然有问题读取令牌文件

# 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) 

我得到这个错误:

Traceback (most recent call last): 
    File "dropb.py", line 14, in <module> 
    token_file = open(TOKENS) 
NameError: name 'TOKENS' is not defined 
+0

错误“'名‘TOKENS’没有defined'”说,这一切:这是因为在你的编辑代码中,你首先使用了几行代码“'TOKENS ='dropbox_token.txt''”,即''token_file = open(TOKENS)''“ ...在第一条使用线出现之前,先在代码中移动定义线。 – sdaau

+0

你真的想与互联网分享APPKEY和APPTOKEN吗? –

回答

20

你可以写在对的access_token文件:

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

如果你这样做一次,然后后记可以使用该令牌:

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) 
+0

令牌文件被保存,但是我得到这个错误:Traceback(最近呼叫最后): 文件“dropb.py”,第11行,在 token_file =打开(TOKENS) NameError:name'TOKENS'未定义 – alkopop79

+0

真棒解决方案 –