Google contacts import using oauth2.0

后端 未结 2 528
天命终不由人
天命终不由人 2020-12-14 04:54

What possible ways to import google contacts using python and oauth2.0 exists?

We successfully got credentials, and our application requests access

相关标签:
2条回答
  • 2020-12-14 05:34

    The Google Contacts API can't be used with the google-api-python-client library because it is a Google Data API, while google-api-python-client is intended to be used with discovery-based APIs.

    Rather than going through all the trouble described by @NikolayFominyh, you can use the native support for OAuth 2.0 in gdata-python-client.

    To get a valid token, follow the instructions from a Google Developers blog post for an in-depth description of the process.

    First, create a token object:

    import gdata.gauth
    
    CLIENT_ID = 'bogus.id'  # Provided in the APIs console
    CLIENT_SECRET = 'SeCr3Tv4lu3'  # Provided in the APIs console
    SCOPE = 'https://www.google.com/m8/feeds'
    USER_AGENT = 'dummy-sample'
    
    auth_token = gdata.gauth.OAuth2Token(
        client_id=CLIENT_ID, client_secret=CLIENT_SECRET,
        scope=SCOPE, user_agent=USER_AGENT)
    

    Then, authorize your application with this token:

    APPLICATION_REDIRECT_URI = 'http://www.example.com/oauth2callback'
    authorize_url = auth_token.generate_authorize_url(
        redirect_uri=APPLICATION_REDIRECT_URI)
    

    After generating this authorize_url, you (or users of your application) will need to visit it and accept the OAuth 2.0 prompt. If this is within a web application, you can simply redirect, otherwise you'll need to visit the link in a browser.

    After authorizing, exchange the code for a token:

    import atom.http_core
    
    redirect_url = 'http://www.example.com/oauth2callback?code=SOME-RETURNED-VALUE'
    url = atom.http_core.ParseUri(redirect_url)
    auth_token.get_access_token(url.query)
    

    In the case you visited a browser, you'll need to copy the URL you were redirected to into the variable redirect_url.

    In the case you are in a web application, you will be able to specify the handler for the path /oauth2callback (for example) and simply can retrieve the query parameter code to exchange the code for a token. For example, if using WebOb:

    redirect_url = atom.http_core.Uri.parse_uri(self.request.uri)
    

    Finally authorize your client with this token:

    import gdata.contacts.service
    
    client = gdata.contacts.service.ContactsService(source='appname')
    auth_token.authorize(client)
    

    Update (12+ months after original answer):

    Alternately you can use the google-api-python-client support as I describe in a blog post.

    0 讨论(0)
  • 2020-12-14 05:38

    Final solution was relatively easy.

    Step 1 Obtain oauth2.0 token. It's pretty documented in official docs: http://code.google.com/p/google-api-python-client/wiki/OAuth2

    Step 2 Now we have token, but can not discover contacts API. But you can find, that in oauth2.0 playground you can import contacts. https://code.google.com/oauthplayground/

    You can find, that you have access token in credentials, obtained in Step 1. To access contacts api you must add to headers following param 'Authorization':'OAuth %s' % access_token

    Step 3 Now you must pass to google library token, that will be compatible with oauth1.0 token. It can be done by following code:

    from atom.http import ProxiedHttpClient #Google contacts use this client
    class OAuth2Token(object):
        def __init__(self, access_token):
            self.access_token=access_token
    
        def perform_request(self, *args, **kwargs):
            url = 'http://www.google.com/m8/feeds/contacts/default/full'
            http = ProxiedHttpClient()
            return http.request(
                'GET',
                url,
                headers={
                    'Authorization':'OAuth %s' % self.access_token
                }
            )
    google = gdata.contacts.service.ContactsService(source='appname')
    google.current_token = OAuth2Token(oauth2creds.access_token)
    feed = google.GetContactsFeed()
    
    0 讨论(0)
提交回复
热议问题