kulpa / google-api-python-client

Automatically exported from code.google.com/p/google-api-python-client
Other
0 stars 0 forks source link

Add sample for server-only OAuth flow for the Coordinate API #196

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
[Use this form for both apiclient and oauth2client issues]

What steps will reproduce the problem?
1. Try to use 
http://code.google.com/p/google-api-python-client/source/browse/samples/coordina
te/coordinate.py to send a request from an environment with no web browser 
(e.g. ssh into a web server that has no web browser installed)
2. credentials = run(FLOW, storage) will fail.

What is the expected output? What do you see instead?

Either this script or an additional script should accept a pair of 
access+refresh tokens, e.g. from the OAuth playground, stored in a text file, 
and use them to get a new (fresh) access token to query the Coordinate API.

What version of the product are you using? On what operating system?
Python 2.6 on Linux.

Please provide any additional information below.
I think the same can be re-used for other APIs, e.g. the Fusion Tables SQL API 
v1.

Original issue reported on code.google.com by mig...@google.com on 18 Sep 2012 at 1:47

GoogleCodeExporter commented 9 years ago
All command-line samples that use tools.run support the 

  --noauth_local_webserver

which allows the user to cut and paste the code w/o requiring a web browser 
running on the same machine as the command line sample.

I would avoid using the oauth2 playground because if you forget to enter your 
own client id, then at midnight all the tokens it's generated get revoked.

There is a sample, not in the samples directory yet, that makes it easier to 
get a refresh token:

   https://codereview.appspot.com/5362041/

Original comment by jcgregorio@google.com on 18 Sep 2012 at 2:11

GoogleCodeExporter commented 9 years ago
Thanks for the hint. I tried that and got a prompt on the shell asking me to 
load a URL on a browser, which is far from my goal here.

I think I wasn't clear enough. It's not about an environment without a browser, 
but also without a human ready to interact at all. Think of a cronjob querying 
the Coordinate API every minute to update another system, once set up and 
running you don't want to require a human to be there once a day.

What I need is a script+json sample that will (once the json is populated with 
good details) succeed in querying the Coordinate API without asking for any 
additional input on the prompt or browser. In other words, I need to use an 
installed app client id "like it was" a service account client id (on an API 
that does not accept service account client ids, e.g. Coordinate API). 

Prompt I got:
$ ./coordinate_make_request.py --api_method='teams/yadayadayadayadayada/jobs' 
--client_id='1234512345.apps.googleusercontent.com' 
--client_secret='yadayadayadayadayada' --credentials_cache=/tmp/test.json 
--noauth_local_webserver
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=1234512345.apps.googleusercontent.com&approval_prompt=force&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcoordinate&access_type=offline

Code I run:
  FLOW = OAuth2WebServerFlow(
      client_id=FLAGS.client_id,
      client_secret=FLAGS.client_secret,
      scope=FLAGS.scopes,
      user_agent='get-refresh-token/1.0',
      approval_prompt='force')  # setting this to 'offline' or removing it made no difference.
  storage = Storage(FLAGS.credentials_cache)
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = run(FLOW, storage)
  http = httplib2.Http()
  http = credentials.authorize(http)
  # then use http.request() with appropriate arguments.

If this is just:
  storage = Storage(FLAGS.credentials_cache)
  credentials = storage.get()  # assuming they are good, see below
  http = httplib2.Http()
  http = credentials.authorize(http)

Then I can call http.request() without any additional steps. However, this does 
require using the OAuth playground which is OK as a one-time (first-time) setup 
step, but not OK to repeat every day.

{"_module": "oauth2client.client",
 "_class": "OAuth2Credentials",
 "access_token": "ya29.yadayadayadayadayadayadayadayadayadayadayadayada",
 "token_uri": "https://accounts.google.com/o/oauth2/token",
 "invalid": true,
 "client_id": "1234512345.apps.googleusercontent.com",
 "id_token": null,
 "client_secret": "yadayadayadayadayada",
 "token_expiry": "2012-07-30T15:37:37Z",
 "refresh_token": "1/yadayadayadayadayadayadayadayadayadayadayada",
 "user_agent": "test/1"}

Original comment by mig...@google.com on 18 Sep 2012 at 2:59

GoogleCodeExporter commented 9 years ago
"""
What I need is a script+json sample that will (once the json is populated with 
good details) succeed in querying the Coordinate API without asking for any 
additional input on the prompt or browser.
"""

Yes, you run https://codereview.appspot.com/5362041/ once from your desktop and 
it will write out a 'cmd-line.dat' file that can then be used from a server:

  storage = Storage('cmd-line.dat')
  credentials = storage.get()

Original comment by jcgregorio@google.com on 18 Sep 2012 at 3:51

GoogleCodeExporter commented 9 years ago
Got it. Perfect! :)

Original comment by mig...@google.com on 20 Sep 2012 at 11:09