Reputation: 1443
I have combined dropbox and google drive in a little app which allow yo to select a dropbox file and upload it to google drive but i have yo copy an authorization code twice every time I use this app and this is not very comfortable,how can I avoid that or get the authorization code once not every time I use it?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'me'
import dropbox
import httplib2
import pprint
from funciones import *
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
import os
from aux import *
import requests
from StringIO import StringIO
#keys dropbox
app_key = 'mykey'
app_secret = 'mysecret'
#keys drive
CLIENT_ID = 'myid'
CLIENT_SECRET = 'mysecret'
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
#parte dropbox
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
access_token, user_id = flow.finish(code)
client = dropbox.client.DropboxClient(access_token)
#print 'linked account: ', client.account_info()
folder_metadata = client.metadata('/')
#print "metadata:", folder_metadata
#print folder_metadata['contents'][0]['path']
print 'Archivos del directorio'
print '***********************'
for files in folder_metadata['contents']:
f = files['path']
print f[1:]
downloaded = raw_input("Introduzca el nobre del fichero que desea subir a google drive: ").strip()
f, metadata = client.get_file_and_metadata('/'+downloaded)
#print 'metadata: ', metadata['mime_type']
out = open(downloaded, 'wb')
out.write(f.read())
out.close()
#parte drive
# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Path to the file to upload
FILENAME = downloaded
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE,
redirect_uri=REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
# Insert a file
media_body = MediaFileUpload(FILENAME, mimetype=metadata['mime_type'], resumable=True)
body = {
'title': 'Subido desde dropbox',
'description': 'A test document',
'mimeType': metadata['mime_type']
}
file = drive_service.files().insert(body=body, media_body=media_body).execute()
pprint.pprint(file)
retrieve_all_files(drive_service)
os.remove(downloaded)
Upvotes: 0
Views: 624
Reputation: 60143
The story here is different between Google and Dropbox. I'm more confident of the Dropbox side (since I work there):
access_token, user_id = flow.finish(code)
, you can save that access_token
and reuse it any time that same user wants to use the app. (Just do client = dropbox.client.DropboxClient(access_token)
with the saved token.)Upvotes: 3