Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
381 views
in Technique[技术] by (71.8m points)

python - Google contacts import using oauth2.0

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

We successfully got credentials, and our application requests access to contacts, but after getting credentials I can't find way to discover contacts api.

So things like:

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

Gives us UnknownApiNameOrVersion exception. It looks like Contacts API not in list of supported APIs for apiclient.

I'm looking for alternative ways.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...