2012-05-11 9 views
7

Quiero evitar tener que autorizar este script una y otra vez. En otras palabras, cuando abro el script desde la terminal, me da un enlace que tengo que abrir en un navegador, luego hago clic en el botón 'Permitir' en el navegador y luego regreso a la terminal ... Supongo que hay una manera para guardar los detalles de autenticación, pero ¿cómo?python dropbox api - ¿Guardar archivo token?

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

------------------------------------------- -------------------------------------------------EDITAR

que modificó el guión y es creado el guión sin embargo todavía tengo problemas para leer el archivo testigo de

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

consigo este error:

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

el error "' nombre 'indicios' no está defined'" lo dice todo: eso es porque en su código de edición, usted ha escrito la definición "' TOKENS = 'dropbox_token.txt'' "unas líneas después de haberla usado primero, que es la línea" 'token_file = open (TOKENS)' " ... Simplemente mueva la línea de definición más temprano en su código, antes de que aparezca la primera línea de uso. – sdaau

+0

¿De verdad quieres compartir la APLICACIÓN y el APPTOKEN con Internet? –

Respuesta

20

puede escribir la señal_acceso a un archivo:

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

Si lo haces una vez, luego epílogos puede utilizar esa señal:

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

El archivo de token se guarda sin embargo me dio este mensaje: Rastreo (llamada más reciente pasado): Archivo "dropb.py", línea 11, en token_file = abierto (fichas) NameError: nombre 'indicios' no está definido – alkopop79

+0

increíble solución –

Cuestiones relacionadas