I've got a Python program that interacts with Google APIs that authenticates via OAuth with the sample code that Google provides:
# Perform OAuth 2.0 authorization.
flow = oauth2client.client.flow_from_clientsecrets(
parameters[self.PARAM_SECRETS], scope=self.GCE_SCOPE)
storage = oauth2client.file.Storage(LocalState.get_oauth2_storage_location(
parameters[self.PARAM_KEYNAME]))
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = oauth2client.tools.run(flow, storage)
# Build the service
return apiclient.discovery.build('compute', self.API_VERSION), credentials
However, whenever I do run this, it forces a browser to open and asks the user to click on a button to authenticate. This causes problems if the program runs on a machine without a local web server. I've seen this question, which seems to indicate that the problem can be solved by using --noauth_local_webserver
, but it says that I have to rewrite my program to use gflags
for argument processing, and my existing code already uses argparse
.
Is there a way to authenticate users without forcing a browser to open, without having to rewrite my whole application to use gflags
?