Search code examples
oauth-2.0google-oauthgoogle-api-python-clientgmail-apioauth2client

Which flag for run_flow() will simulate the now deprecated run()


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)


Solution

  • 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)