How do I authorize a gdata client without using the gdata oauth2 workflow?
Asked Answered
M

3

3

I already have an access_token and refresh_token, but I can't figure out a way to create an authorized gdata client without going through the entire token generation workflow in gdata.

Methodius answered 7/2, 2013 at 2:9 Comment(0)
M
4

So I got this working finally. Here's how I did it:

    client = gdata.contacts.client.ContactsClient()
    credentials = gdata.gauth.OAuth2Token(client_id = 'client_id',
                                          client_secret = 'client_secret',
                                          scope = 'https://www.google.com/m8/feeds/',
                                          user_agent = auth.user_agent, # This is from the headers sent to google when getting your access token (they don't return it)
                                          access_token = auth.access_token,
                                          refresh_token = auth.refresh_token)

    credentials.authorize(client)
    contacts = client.get_contacts()
Methodius answered 7/2, 2013 at 21:52 Comment(2)
where does your auth object come from?Sarcophagus
I was struggling with the same issue and finally somehow combined code that looks pretty much as yours. Thanks anyway. Upping.Grigsby
O
1

Try this:

import httplib2
from oauth2client.client import OAuth2Credentials

credentials = OAuth2Credentials('access_token', client_id, client_secret, 'refresh_token', 'token_expiry','token_uri','user_agent')
# the client_id and client_secret are the ones that you receive which registering the App 
# and the token_uri is the Redirect url you register with Google for handling the oauth redirection
# the token_expiry and the user_agent is the one that you receive when exchange the code for access_token
http = httplib2.Http()
http = credentials.authorize(http)
service = build('analytics', 'v3', http=http) # this will give you the service object which you can use for firing API calls
Oaxaca answered 7/2, 2013 at 2:19 Comment(2)
I don't get any "user_agent" when I get my access token.Methodius
I realized your code won't work for me since I'm using the contacts API at the moment, which isn't supported by their newer library. I didn't realize that mattered or I would have put it in the question.Methodius
E
-1

Gdata allows you to authenticate using user info, ex. username/password... here is a code snipet from the gdata python api /gdata-2.0.18/samples/docs/docs_example.py file that comes with the api

class DocsSample(object): """A DocsSample object demonstrates the Document List feed."""

def init(self, email, password): """Constructor for the DocsSample object.

Takes an email and password corresponding to a gmail account to
demonstrate the functionality of the Document List feed.

Args:
  email: [string] The e-mail address of the account to use for the sample.
  password: [string] The password corresponding to the account specified by
      the email parameter.

Returns:
  A DocsSample object used to run the sample demonstrating the
  functionality of the Document List feed.
"""
source = 'Document List Python Sample'
self.gd_client = gdata.docs.service.DocsService()
self.gd_client.ClientLogin(email, password, source=source)

# Setup a spreadsheets service for downloading spreadsheets
self.gs_client = gdata.spreadsheet.service.SpreadsheetsService()
self.gs_client.ClientLogin(email, password, source=source)

if you invoke this as {python ./docs_example.py --user username --pw password} it will skip asking you for it, but it will ask you for it if you don't. However this is being depreciated but still works in most situations outside networks that directly work with Google as this often times will now require oauth2. This being said, it does have security drawbacks, specifically scope, and poor password protection, which is why it is being deprecated...but that should answer your question a little better...

Exclave answered 19/5, 2015 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.