2012-05-11 25 views
7

Tôi muốn tránh phải cho phép tập lệnh này lặp đi lặp lại. Nói cách khác, khi tôi khởi chạy kịch bản từ thiết bị đầu cuối, nó cung cấp cho tôi một liên kết tôi phải mở trong trình duyệt, sau đó nhấp vào nút 'Cho phép' trong trình duyệt rồi quay lại thiết bị đầu cuối ... Tôi đoán có một cách để lưu các chi tiết xác thực nhưng làm thế nào?python dropbox api - lưu tập tin mã thông báo?

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

------------------------------------------- -------------------------------------------------CHỈNH SỬA

tôi sửa đổi kịch bản và nó tạo ra kịch bản tuy nhiên tôi vẫn còn có vấn đề đọc file thẻ

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

tôi nhận được lỗi này:

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

lỗi "' tên 'thẻ' không defined'" nói lên tất cả: đó là vì trong mã chỉnh sửa của bạn, bạn đã viết định nghĩa "' TOKENS = 'dropbox_token.txt'' "một vài dòng sau khi bạn đã sử dụng nó trước, đó là dòng" 'token_file = open (TOKENS)' " ... Chỉ cần di chuyển dòng định nghĩa trước đó trong mã của bạn, trước khi dòng sử dụng đầu tiên xuất hiện. – sdaau

+0

Bạn có thực sự muốn chia sẻ APPKEY và APPTOKEN với Internet không? –

Trả lời

20

Bạn có thể viết các access_token vào một tệp:

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

Nếu bạn làm điều đó một lần, sau đó bạt bạn có thể sử dụng mã thông báo:

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

Tệp mã thông báo được lưu tuy nhiên tôi nhận được lỗi này: Traceback (cuộc gọi gần đây nhất): Tệp "dropb.py", dòng 11, trong token_file = open (TOKENS) NameError: name 'TOKENS' không được xác định – alkopop79

+0

giải pháp tuyệt vời –

Các vấn đề liên quan