Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58459 > unrolled thread

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

Started byPratik Mehta <pratik.mehta13390@gmail.com>
First post2013-11-04 11:46 -0800
Last post2013-11-06 16:19 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Auto-authorization for Google Drive using Python in Mac OS X Pratik Mehta <pratik.mehta13390@gmail.com> - 2013-11-04 11:46 -0800
    Re: Auto-authorization for Google Drive using Python in Mac OS X Peter Pearson <ppearson@nowhere.invalid> - 2013-11-06 16:19 +0000

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

FromPratik Mehta <pratik.mehta13390@gmail.com>
Date2013-11-04 11:46 -0800
SubjectAuto-authorization for Google Drive using Python in Mac OS X
Message-ID<4324b1fe-7a92-4e06-bedd-1eb4bd2ee738@googlegroups.com>
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.

[toc] | [next] | [standalone]


#58572

FromPeter Pearson <ppearson@nowhere.invalid>
Date2013-11-06 16:19 +0000
Message-ID<bdv8h8Fu7d3U1@mid.individual.net>
In reply to#58459
On Mon, 4 Nov 2013 11:46:54 -0800 (PST), Pratik Mehta wrote:

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


-- 
To email me, substitute nowhere->spamcop, invalid->net.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web