1

I'm trying to use the Google Docs API with Python+Django and OAuth 2. I've got the OAuth access token, etc. via google-api-python-client, with the code essentially copied from http://code.google.com/p/google-api-python-client/source/browse/samples/django_sample/plus/views.py

Now, I assume I should be using the google gdata API, v 2.0.17. If so, I'm unable to find exactly how to authorize queries made using the gdata client. The docs at http://packages.python.org/gdata/docs/auth.html#upgrading-to-an-access-token (which appear outdated anyway), say to set the auth_token attribute on the client to an instance of gdata.oauth.OAuthToken. If that's the case, what parameters should I pass to OAuthToken?

In short, I'm looking for a brief example on how to authorize queries made using the gdata API, given an OAuth access token.

Jordon Wii
  • 151
  • 1
  • 1
  • 8

2 Answers2

5

The OAuth 2.0 sequence is something like the following (given suitably defined application constants for your registered app).

  1. Generate the request token.

    token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID, 
                                    client_secret=CLIENT_SECRET, 
                                    scope=" ".join(SCOPES), 
                                    user_agent=USER_AGENT)
    
  2. Authorise the request token. For a simple command-line app, you can do something like:

    print 'Visit the following URL in your browser to authorise this app:'
    print str(token.generate_authorize_url(redirect_url=REDIRECT_URI))
    print 'After agreeing to authorise the app, copy the verification code from the browser.'
    access_code = raw_input('Please enter the verification code: ')
    
  3. Get the access token.

    token.get_access_token(access_code)
    
  4. Create a gdata client.

    client = gdata.docs.client.DocsClient(source=APP_NAME)
    
  5. Authorize the client.

    client = token.authorize(client)
    

You can save the access token for later use (and so avoid having to do the manual auth step until the token expires again) by doing:

f = open(tokenfile, 'w')
blob = gdata.gauth.token_to_blob(token)
f.write(blob)
f.close()

The next time you start, you can reuse the saved token by doing:

f = open(tokenfile, 'r')
blob = f.read()
f.close()
if blob:
    token = gdata.gauth.token_from_blob(blob)

Then, the only change to the authentication sequence is that you pass this token to OAuth2Token by specifying a refresh_token argument:

token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID, 
                                client_secret=CLIENT_SECRET, 
                                scope=" ".join(SCOPES), 
                                user_agent=USER_AGENT,
                                refresh_token=token.refresh_token)

Hope this helps. It took a while to work it out :-).

jiml
  • 51
  • 2
0

This is from https://developers.google.com/gdata/docs/auth/overview:

Warning: Most newer Google APIs are not Google Data APIs. The Google Data APIs documentation applies only to the older APIs that are listed in the Google Data APIs directory. For information about a specific new API, see that API's documentation. For information about authorizing requests with a newer API, see Google Accounts Authentication and Authorization.

You should either use OAuth for both authorization and access or OAuth 2.0 for both.

For OAuth 2.0 API are now at https://developers.google.com/gdata/docs/directory.

Artem Oboturov
  • 4,344
  • 2
  • 30
  • 48