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.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to