3

I am trying to authenticate my credentials to access the GMail API. Previously I did this using the run() method from OAuth2, and the code credentials = tools.run(flow, STORAGE, http=http) but this is now a deprecated method. I am now using the run_flow() method to authenticate my credentials.

import httplib2
import argparse
from apiclient import errors
from apiclient.discovery import build
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets

CLIENT_SECRET_FILE = 'your_client_secret.json'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.modify'
STORAGE = Storage('gmail.storage')
flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE)
http = httplib2.Http()
credentials = STORAGE.get()there are credentials, no reauth is needed
#parser = argparse.ArgumentParser(parents=[tools.argparser])
#flags = parser.parse_args()    #Put your arguments in the parenthesis
if credentials is None or credentials.access_token_expired:
    credentials = run(flow, STORAGE, http=http)
    #credentials = tools.run_flow(flow, STORAGE, flags, http=http)
http = credentials.authorize(http)
gmail_service = build('gmail', 'v1', http=http)

The commented lines are the code that uses run_flow() and not run().

The commented out code gives me the error: run.py: error: unrecognized arguments: AdminTests, AdminTests is not an argument I give to Python.

And when I change the arguments parsed to flags = parser.parse_args(['--noauth_local_webserver']) I get no error, but nothing happens.

Which flag should I use to simulate the run() as closesly as possible and how should I parse it?

Edit: When using the run() method to authenticate my credentials the URL accessed is: http://localhost:8080/?code=4/myuniqueID (missing my unique ID in the example)

Eric D
  • 6,901
  • 1
  • 15
  • 26
LeonH
  • 1,039
  • 2
  • 22
  • 45
  • more details can be found here... http://stackoverflow.com/questions/24890146/how-to-get-google-analytics-credentials-without-gflags-using-run-flow-instead/25529906#25529906 – John Ruddell Aug 27 '14 at 14:51

2 Answers2

8

what you need to do for this is pass an empty list of args to the argparser like this

flags = tools.argparser.parse_args(args=[])
credentials = tools.run_flow(flow, storage, flags)
John Ruddell
  • 25,283
  • 6
  • 57
  • 86
  • This seems obsolete (at least for Python3): `tools.run_flow()` contains this code: `if flags is None:` `flags = argparser.parse_args()` so no need to do this by hand. – Adrian W May 09 '18 at 12:19
  • As is true with most tech, it evolves and things become obsolete. I haven't looked back at the code, but this answer was ~ 3.5 years ago and it was most definitely in python 2.7 :) – John Ruddell May 09 '18 at 21:17
  • 1
    I need to update my comment: the built-in code will use the global `argv` by default and hence will work only if the global `argv` is empty or contains only arguments the built-in parser recognizes. If you have your own arguments, the built-in parser is likely to fail. Therefore your anwer is still valid and necessary. – Adrian W May 12 '18 at 13:21
-1

After comparing your code to the source code of OAuth's run and run_flow, it turns out that there is a significant difference between whether you include the http argument.

So,

tools.run(flow, STORAGE, http=http)

can be simulated with,

tools.run_flow(flow, STORAGE, flags, http=http)

but you have,

tools.run_flow(flow, STORAGE, flags)
randomusername
  • 7,927
  • 23
  • 50
  • 2
    You haven't even read the question properly.. Flags are arguments taken at the command line (and are given [here](https://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.tools-module.html) to give directions to the storage you are using for your credentials and the method you wish to use to access/authenticate them. – LeonH Aug 26 '14 at 15:57