Chcę uniknąć konieczności autoryzowania tego skryptu w kółko. Innymi słowy, po uruchomieniu skryptu z terminala, daje mi link, który muszę otworzyć w przeglądarce, a następnie kliknij przycisk "Zezwalaj" w przeglądarce, a następnie wróć do terminalu ... Myślę, że jest sposób zapisać szczegóły uwierzytelniania, ale w jaki sposób?python dropbox api - zapisać plik tokenu?
# 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)
------------------------------------------- -------------------------------------------------EDYTOWAĆ
zmodyfikowałem skrypt i stworzył scenariusz jednak nadal mam problemy z czytaniem pliku tokena
# 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)
otrzymuję ten błąd:
Traceback (most recent call last):
File "dropb.py", line 14, in <module>
token_file = open(TOKENS)
NameError: name 'TOKENS' is not defined
błąd „' nazwa «znaków» nie jest defined'”mówi wszystko: Dzieje się tak dlatego, że w swoim kodzie edycji napisałeś definicję "' TOKENS = 'dropbox_token.txt'' "kilka linii po pierwszym użyciu, co jest linią" 'token_file = open (TOKENS)' " ... Po prostu przesuń linię definicji wcześniej w kodzie, zanim pojawi się pierwsza linia użycia. – sdaau
Czy naprawdę chcesz udostępnić APPKEY i APPTOKEN w Internecie? –