Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store access token from Google OAuth 2.0 to access drive data from application account

I am able to execute quickstart.py for Google Drive using python. But how do we store the token and use it again - again for the same user without prompting user. Is their some way i can map user with access token when sending request for file on Google drive.

like image 438
Navi Avatar asked Mar 19 '13 06:03

Navi


People also ask

How do I get OAuth tokens on Google Drive?

Procedure. Go to Google Developers OAuth Playground. Click OAuth 2.0 Configuration and select Use your own OAuth credentials check box, enter the OAuth client ID and client secret you have already created in the OAuth Client ID and OAuth Client secret fields respectively.

Where is OAuth2 token stored?

Tokens received from OAuth providers are stored in a Client Access Token Store. You can configure client access token stores under the Libraries > OAuth2 Stores node in the Policy Studio tree view.


2 Answers

There are many different Storage types offered by google-api-python-client, some of which are well documented.

Some examples:

oauth2client.file.Storage:

from oauth2client.file import Storage
...
storage = Storage('a_credentials_file')
storage.put(credentials)
...
credentials = storage.get()

oauth2client.keyring_storage.Storage:

from oauth2client.keyring_storage import Storage
...
storage = Storage('application name', 'user name')
storage.put(credentials)
...
credentials = storage.get()

oauth2client.appengine.StorageByKeyName:

from oauth2client.keyring_storage import StorageByKeyName
from oauth2client.keyring_storage import CredentialsNDBModel
...
storage = StorageByKeyName(CredentialsNDBModel, some_user_id, 'credentials')
storage.put(credentials)
...
credentials = storage.get()

oauth2client.django_orm.Storage:

from django.contrib.auth.models import User
from oauth2client.django_orm import Storage
from your_project.your_app.models import CredentialsModel
...
user = # A User object usually obtained from request.
storage = Storage(CredentialsModel, 'id', user, 'credential')
credential = storage.get()
...
storage.put(credential)
like image 70
bossylobster Avatar answered Sep 30 '22 19:09

bossylobster


I think you should give credit to bossylobster for a more complete answer, but based on your comment, which is precisely my setup, I've augmented the quickstart.py using the Storage class:

#!/usr/bin/python                                                                                                                         
import httplib2
import pprint

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage

# Copy your credentials from the console                                                                                                  
CLIENT_ID = 'PASTE_YOUR_ID'
CLIENT_SECRET = 'PASTE_YOUR_SECRET'

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

# Create a credential storage object.  You pick the filename.
storage = Storage('a_credentials_file')

# Attempt to load existing credentials.  Null is returned if it fails.
credentials = storage.get()

# Only attempt to get new credentials if the load failed.
if not credentials:

    # Run through the OAuth flow and retrieve credentials                                                                                 
    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)
    storage.put(credentials)


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

# Use 'drive_service' for all of the API calls
like image 30
D. A. Avatar answered Sep 30 '22 18:09

D. A.