Pydrive authentication using
Question: 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.


Answer: 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](http://console.developers.google.com)

*.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, something@developer.gserviceaccount.com,
EMAIL_ID@YOURDOMAIN.COM in order to run it properly and test it.

Hope this will help! Resource- [Google Drive
API](https://developers.google.com/drive/web/delegation)

    
    
    import httplib2
    import pprint
    import sys
    
    from apiclient.discovery import build
    from oauth2client.client import SignedJwtAssertionCredentials
    
    """Email of the Service Account"""
    SERVICE_ACCOUNT_EMAIL = 'something@developer.gserviceaccount.com'
    
    """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_ID@YOURDOMAIN.COM')
    
    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"
    

