phenomenon.aurora
phenomenon.aurora

Reputation: 521

Pydrive authentication using

I was using gdata module to access, upload, download files from google doc. I have the oauth key and secret with me. Now I want to switch to google drive api. Learning and studying a bit on google drive api , it looks like a bit different in the authentication. I also have downloaded pydrive module so as I can start things up. But I am not able to authorize my server side python code to authorize/authenticate the user using my oauth keys and access my drive. Do any one has any spare know how on how I can use pydrive to access my drive with my previous auth keys. I just need a simple way to authenticate.

Upvotes: 0

Views: 1676

Answers (1)

Jai
Jai

Reputation: 41

For using the gdata module we use either of these credentials- 1> username & password or 2> consumer oauth key and secret key.

Since you are trying to use oauth credentials, I think you want a Domain Wide Delegated Access for Google Drive, which will help you to achieve uploading/downloading files into any user's google drive through out the domain.

For this you need to generate a new Client ID of a Service Account Type from Developer's Console

*.p12 file will get downloaded. Note the path where you save it. Also note the email address of your Service account. These will be use while coding.

Below is the python code where u have to carefully edit- PATH TO SERIVE ACCOUNT PRIVATE KEY, [email protected], [email protected] in order to run it properly and test it.

Hope this will help! Resource- Google Drive API

import httplib2
import pprint
import sys

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

"""Email of the Service Account"""
SERVICE_ACCOUNT_EMAIL = '[email protected]'

"""Path to the Service Account's Private Key file"""
SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'PATH TO SERIVE ACCOUNT PRIVATE KEY'

def createDriveService(user_email):
  """Build and returns a Drive service object authorized with the service accounts
  that act on behalf of the given user.

  Args:
    user_email: The email of the user.
  Returns:
    Drive service object.
  """
  f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
  key = f.read()
  f.close()

  credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
      scope='https://www.googleapis.com/auth/drive', sub=user_email)
  http = httplib2.Http()
  http = credentials.authorize(http)

  return build('drive', 'v2', http=http)

drive_service=createDriveService('[email protected]')

result = []
page_token = None


while True:
    try:
        param = {}
        if page_token:
            param['pageToken'] = page_token
        files = drive_service.files().list().execute()
        #print files
        result.extend(files['items'])
        page_token = files.get('nextPageToken')
        if not page_token:
            break
    except errors.HttpError, error:
        print 'An error occurred: %s' % error
        break

for f in result:
    print '\n\nFile: ',f.get('title')

    print "\n"

Upvotes: 1

Related Questions