本文介绍了Google联系人使用oauth2.0导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python和 oauth2.0 导入Google联系人的方法有哪些?



我们成功获取凭证,并且我们的应用程序请求访问联系人,但获得凭据后,我无法找到方式来发现联系人的API。



例如:

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

给我们 UnknownApiNameOrVersion 异常。
它看起来像Contacts API不在受支持的API列表中。



我正在寻找其他方法。 不能与 google-api-python-client 库一起使用,因为它是,而 google-api-python-client 旨在用于。



与所描述的所有问题相比, a>,您可以在 gdata-python-client 中使用对OAuth 2.0的本机支持。 请按照Google Developers 。


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.

解决方案

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.

这篇关于Google联系人使用oauth2.0导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:01