Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-authorization for Google Drive using Python in Mac OS X

I have written Python code for Google Drive which uploads the image files to my drive app. I have three queries. Here is my code:

#!/usr/bin/python

import httplib2
import pprint

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
from oauth2client.client import OAuth2WebServerFlow
from apiclient import errors
import sys


CLIENT_ID = 'CLIENT_ID'
CLIENT_SECRET = 'CLIENT_SECRET'

OAUTH_SCOPE = ['https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive.file']

REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

FILENAME = "filepath/filename.png"

flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
flow.params['access_type'] = 'offline'
flow.params['approval_prompt'] = 'force'
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)

media_body = MediaFileUpload(FILENAME, mimetype='image/png', resumable=True)
body = {
    'title': 'Screen Shot 2013-11-03 at 3.54.08 AM',
    'description': 'A test screenshot',
    'mimeType': 'image/png'
}

file = drive_service.files().insert(body=body, media_body=media_body).execute()

new_permission = {
      'type': 'anyone',
      'role': 'reader'
}

try:
    drive_service.permissions().insert(
        fileId=file['id'], body=new_permission).execute()
except errors.HttpError, error:
    print 'An error occurred: %s' % error

pprint.pprint(file)

My Queries:

  1. This program will upload all the images to my given client_id and client_secret.
    How do I make users to use my app and upload their images to their own Google Drive?

  2. I want to automate this task. Whenever I run this application in terminal, it always asks me for the authorization code, which I don't want. Can this be bypassed?

  3. I read about refresh_tokens, but couldn't find how can I implement this in my app for automating the authorization.
    So, is refresh_tokens used for that? If yes, then how do I implement it in my program?
    If not, then how can I make sure that as soon as my application is loaded, that particular file gets uploaded on google drive directly, without any authorization, or with any auto-authorization way, so that user interaction is chucked completely.

like image 691
user2927391 Avatar asked Jun 20 '26 16:06

user2927391


1 Answers

"This program will upload all the images to my given client_id and client_secret."

No, it will upload the images to Drive for the account that goes through the authorization flow. The Client ID and Client Secret identity your program, they don't identify a particular user.

"I want to automate this task. Whenever I run this application in terminal, it always asks me for the authorization code, which I don't want. Can this be bypassed?"

Store the credentials in a Storage:

https://developers.google.com/api-client-library/python/guide/aaa_oauth#storage

The oauth2client library handles refresh tokens for you, the piece you are missing is storing the credentials in a Storage. The user will still need to run through the authorization process the first time, but after that it should just work w/o any interaction. Look at tools.run_flow() to handle most of this for you:

http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.tools-module.html#run_flow

See the samples for how this is used:

https://code.google.com/p/google-api-python-client/source/browse/samples/plus/plus.py#32

like image 163
Joe Gregorio Avatar answered Jun 22 '26 06:06

Joe Gregorio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!