0

At the moment i am needing to get a "success code" each time i want to run my .py app. to access my googdrive files which is a pain.

I saw Ali Afshars great vid on https://developers.google.com/drive/search-parameters where he uses: from auth import http to streamline this process.

Not sure what this function should contain... Can you point me in the right direction, so that i can set up my .py to do this automatically ...or at least only once.

many thanks

Dav-o


EDIT relevant current snip of code follows:

import logging
logging.basicConfig()
import httplib2
import pprint
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import *
from apiclient import errors

CLIENT_ID = "864350......ps.googleusercontent.com"
CLIENT_SECRET = "sw0yb.....-zR6XWzEgM"
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive' 
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oo...ooo' # Redirect URI for installed apps

flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, 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)

http = httplib2.Http() 
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)
4

1 回答 1

2

What he probably does in auth is:

import httplib2
from oauth2client.client import OAuth2Credentials

http = httplib2.Http()
credentials = OAuth2Credentials(access_token, client_id, client_secret, refresh_token, None, '', '')
credentials.authorize(http)

You can wrap this code in a module/method and use the authenticate your requests. For multiple user scenarios, preserve access and refresh tokens (most likely in a db) and init credentials object with the stored tokens.

于 2013-04-17T08:40:14.007 回答