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

2

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)

Bollard answered 21/8, 2014 at 9:16 Comment(1)
more details can be found here... #24890646Stichometry
S
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)
Stichometry answered 2/1, 2015 at 1:6 Comment(3)
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.Lysimachus
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 :)Stichometry
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.Lysimachus
D
-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)
Domingodominguez answered 26/8, 2014 at 15:32 Comment(1)
You haven't even read the question properly.. Flags are arguments taken at the command line (and are given here to give directions to the storage you are using for your credentials and the method you wish to use to access/authenticate them.Bollard

© 2022 - 2024 — McMap. All rights reserved.