How to get OAuth credentials using Google Drive SDK 2 and Python without human interaction

I have a Python application that extracts data from a third-party source, selects a subset and writes it to a CSV file, which is then uploaded to Google Drive and converted to a spreadsheet. I want to run this as a cron job, but currently the SDK requires a person to interact with the browser to get OAuth credentials.

I searched the SDK Drive documents and did not find anything to help. I also tried unsuccessfully to automate the OAuth process using a mechanization library. I'm sure I missed something. I can’t believe that the Drive API requires human interaction. Suggestions?

I consulted with https://developers.google.com/accounts/docs/OAuth2WebServer#overview and this is what I tried.

flow = OAuth2WebServerFlow(settings.CLIENT_ID, settings.CLIENT_SECRET setings.OAUTH_SCOPE,settings.REDIRECT_URI) authorize_url = flow.step1_get_authorize_url() 

creates the request url:

 https://accounts.google.com/o/oauth2/auth? scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive& redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob& response_type=code& client_id=999637210521.apps.googleusercontent.com& access_type=offline 

Then, using the query library, I output:
r = req.get(authorize_url)

but the response body is an HTML document without code.

+4
source share
3 answers

I also have the same problem earlier.

Decision. Save your credentials for future use. Refer to the following URLs:

https://developers.google.com/accounts/docs/OAuth2#installed

https://developers.google.com/accounts/docs/OAuth2InstalledApp

 from oauth2client.file import Storage ... storage = Storage('a_credentials_file') storage.put(credentials) ... credentials = storage.get() 

I clicked on my script in which I am reusing credentials to create new tokens for github.

https://github.com/sukujgrg/google_drive

+1
source

Have you looked at the Google Drive API authorization section? ( https://developers.google.com/drive/examples/python#authorization )

If this does not give you what you need, look in the requests library. It is easy to use and should allow you to easily get your OAuth loans.

0
source

RENOUNCEMENT:

I am not a security expert and I don’t know the risks associated with my terrible solution to the problem ... but this will lead to Cron running the code.

If you are ready to do manual manual reception once for your application with a specific scope, as in the Google quick-start example (useful parts located at the bottom of the answer): https://developers.google.com/drive/web/quickstart/python

You can then add both the CLIENT_SECRET_FILE AND drive-quickstart.json files that you created once to your repository. Then, if you have a Google account in which you always want ANY copy of your code base to have access to it, there will never be a second credential check.

DENIAL OF RESPONSIBILITY:

Intuitively, this solution is terrible for security, and the moment your code base is published ANY way, your Google drive is completely at risk of destruction, deletion, ban, etc.

 import httplib2 import os from apiclient import discovery import oauth2client from oauth2client import client from oauth2client import tools def get_credentials(): try: import argparse flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() except ImportError: flags = None SCOPES = 'https://www.googleapis.com/auth/drive' #ASSUME THAT THE CLIENT SECRETE FILE BELOW IS STORED IN YOUR CODE BASE CLIENT_SECRET_FILE = 'client_secret.json' APPLICATION_NAME = 'Drive API Quickstart' #ASSUME THAT THIS FILE BELOW IS STORED INSIDE YOUR CODE BASE credential_dir = os.path.realpath('') credential_path = os.path.join(credential_dir, 'drive-quickstart.json') store = oauth2client.file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) flow.user_agent = APPLICATION_NAME if flags: credentials = tools.run_flow(flow, store, flags) else: # Needed only for compatability with Python 2.6 credentials = tools.run(flow, store) print('Storing credentials to ' + credential_path) return credentials def oneTimeRunCredentials(): credentials = get_credentials() http = credentials.authorize(httplib2.Http()) service = discovery.build('drive', 'v2', http=http) results = service.files().list(maxResults=10).execute() 
0
source

All Articles