Import Google contacts using oauth2.0

What are the ways to import google contacts using python and oauth2.0 ?

We have successfully obtained the credentials, and our application requests access to the contacts, but after receiving the credentials I can’t find a way to detect the api contacts.

So things like:

from apiclient.discover import build import httplib2 http = httplib2.Http() #Authorization service = build("contacts", "v3", http=http) 

UnknownApiNameOrVersion exception. It looks like the contact API is not on the list of supported APIs for apiclient.

I am looking for alternative ways.

+8
python google-api contacts google-api-python-client
source share
2 answers

The Google Contacts API cannot be used with the google-api-python-client library, since it is a Google data API , and google-api-python-client intended to be used with the discovery-based API .

Instead of overcoming all the problems described by @NikolayFominyh , you can use the built-in OAuth 2.0 support in gdata-python-client .

To get a valid token, follow the instructions in the Google Blog Report for a detailed 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 using this token:

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

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

After authorization, replace the code for the 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 case you visited the browser, you need to copy the URL to which you are redirected to the redirect_url variable.

In case you are in a web application, you can specify a handler for the /oauth2callback (for example) and you can just get the code request parameter to exchange the code for the token. For example, if you use WebOb :

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

Finally, authorize your client using this token:

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

Update (12 months after the initial response):

Alternatively, you can use google-api-python-client support, as I described in the post.

+20
source share

The final decision was relatively simple.

Step 1 Get the oauth2.0 token. This is well documented in white papers: http://code.google.com/p/google-api-python-client/wiki/OAuth2

Step 2 Now we have a token, but it cannot detect the contact API. But you may find that on the oauth2.0 playground you can import contacts. https://code.google.com/oauthplayground/

You may find that you have the credential access token obtained in step 1. To access the api contacts, you must add the following 'Authorization':'OAuth %s' % access_token parameters to the headers 'Authorization':'OAuth %s' % access_token

Step 3 Now you should go to the google library token, which will be compatible with the oauth1.0 token. This can be done using the 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() 
+2
source share

All Articles