sixD
sixD

Reputation: 227

Need to get OAuth 'flow' flowing for Google Drive on Python for a stand alone py app

I need to get OAuth2 'flow' for GoogDRIVE working.

Previously I have gotten the success token thing to work fine, but I need to have it set up so I don't need to get the token each time, just once would be fine.

Here is where I'm at, based on: https://developers.google.com/api-client-library/python/guide/aaa_oauth

import httplib2
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from apiclient.discovery import build

flow = OAuth2WebServerFlow(client_id='107......539.apps.googleusercontent.com',
                       client_secret='dVvq2itsasecretbxzG',
                       scope='https://www.googleapis.com/auth/drive',
                       redirect_uri='urn:ietf:wg:oauth:2.0:oob')

#retrieve if available
storage = Storage('OAuthcredentials.txt')
credentials = storage.get()

if  credentials is None:
    #step 1
    auth_uri = flow.step1_get_authorize_url() # Redirect the user to auth_uri
    print 'Go to the following link in your browser: ' + auth_uri
    code = raw_input('Enter verification code: ').strip()
else:
    code = credentials #yeah i know this bit is wrong, but pls send HELP!

#step 2
credentials = flow.step2_exchange(code)

#authorise 
http = httplib2.Http()
http = credentials.authorize(http)
print 'authorisation completed'

#build
service = build('drive', 'v2', http=http)

#store for next time
storage.put(credentials)

Its for a stand-alone app. so firstly am I going about this the right way? And if so, how do I enter the credentials as the code in the above else?

Upvotes: 2

Views: 1100

Answers (1)

sixD
sixD

Reputation: 227

Yep it was a simple mistake. Here is the working snip to replace the bits in the Q above:

......
if  credentials is None:
    #step 1
    auth_uri = flow.step1_get_authorize_url() # Redirect the user to auth_uri
    print 'Go to the following link in your browser: ' + auth_uri
    code = raw_input('Enter verification code: ').strip()
    #step 2
    credentials = flow.step2_exchange(code)
else:
    print 'GDrive credentials are still current'

#authorise
http = httplib2.Http()
http = credentials.authorize(http)
print 'Authorisation successfully completed'
......etc

cheers

Upvotes: 2

Related Questions