2012-05-11 4 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

오류 "'이름 '토큰'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() 

한 번, 당신은 그 토큰을 사용할 수 afterwords 그렇게한다면 :

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.11 토큰 파일 이름 : – alkopop79

+0

멋진 솔루션 –