Question

I am trying to get the oauth from Google, After authentication every time I need to copy and paste the Code Generated in my console ! I want that to be automate it ? Any idea how can I do that ?

I have this code !

DFA_USER_PROFILE_NAME='MYPROFILE_NAME'

scope = ["https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/dfareporting"]

try:

   redirect_uri=redirect_uri='urn:ietf:wg:oauth:2.0:oob'
   flow=flow_from_clientsecrets('Location of the json file', scope, redirect_uri, message)
   authorize_url = flow.step1_get_authorize_url()
   self.redirect(authorize_url)
   credential = flow.step2_exchange(self.request.params)
   storage=StorageByKeyName(credentials,user.user_id(),'credentials'
                         )
   storage.put(credentials)
   user=user.get_current_user()

   credentials = client.oauth2credentials
   client.oauth2credentials = credentials
   http = httplib2.Http()

except BaseException as e: print "this is really a error that has occured ",e

I am trying But i have no clue where I am going wrong ?

what is self.params ? Is this service free in Google Cloud ?

Was it helpful?

Solution

Try the following - it should prompt you the first time, but then write the credentials to disk and read them in for subsequent invocations:

import httplib2
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run_flow, argparser
from apiclient.discovery import build

storage = Storage("/path/to/saved_user_creds.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
  credentials = run_flow(flow_from_clientsecrets("/path/to/client_secrets.json", scope=["scope1" ,"scope2"]), storage, argparser.parse_args([]))
http = credentials.authorize(httplib2.Http())

# Use the http object as needed...
service = build("bigquery", "v1")
result = service.object().method(name=value).execute(http=http)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top