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


P

Pratik Mehta

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:eek:auth:2.0:eek:ob'

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.
 
Ad

Advertisements

P

Peter Pearson

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: [snip]

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.

I'm sorry to see that you haven't gotten a response. I can't help,
because I can't understand the questions, perhaps because they
require intimate knowledge of Google Drive or of a library that
you're using to access Google Drive. This is ordinarily an
amazingly helpful group; perhaps you could find a more specific
question that could be illustrated with shorter code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top