Google联系人使用oauth2.0导入 [英] Google contacts import using 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列表中。
我正在寻找其他方法。 rel =nofollow noreferrer> Google Contacts API 不能与 google-api-python-client
库一起使用,因为它是 Google数据API ,而 google-api-python-client
旨在用于基于发现的API 。
与 @NikolayFominyh 所描述的所有问题相比, a>,您可以在 gdata-python-client
中使用对OAuth 2.0的本机支持。 请按照Google Developers
首先,创建一个令牌对象:
import gdata.gauth
CLIENT_ID ='bogus.id'#在API控制台中提供
CLIENT_SECRET = 'SeCr3Tv4lu3'#在API控制台中提供
SCOPE ='https://www.google.com/m8/feeds'
USER_AGENT ='dummy-sample'
auth_token = gdata.gauth.OAuth2Token(
client_id = CLIENT_ID,client_secret = CLIENT_SECRET,$ b $ scope = SCOPE,user_agent = USER_AGENT)
然后,使用此令牌授权您的应用程序:
APPLICATION_REDIRECT_URI ='http:// www.example.com/oauth2callback'
authorize_url = auth_token.generate_authorize_url(
redirect_uri = APPLICATION_REDIRECT_URI)
产生这个 authorize_url
后,你(或你的应用程序的用户)将需要访问它并且加入在OAuth 2.0提示符下。如果这是在一个Web应用程序中,您可以简单地重定向,否则您需要访问浏览器中的链接。
授权后,将代码交换为令牌:
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)
在访问浏览器的情况下,您需要将您重定向到的URL复制到变量 redirect_url
。
如果您处于Web应用程序中,您将能够为路径指定处理程序 / oauth2callback
(例如),并且可以简单地检索查询参数 code
来交换令牌的代码。例如,如果使用 WebOb
:
redirect_url = atom.http_core。 Uri.parse_uri(self.request.uri)
最后使用此令牌授权您的客户端:
import gdata.contacts.service
client = gdata.contacts.service.ContactsService(source ='appname' )
auth_token.authorize(客户端)
更新(原始答案后12个月以上):
或者,您可以使用 google-api-python-client
支持,正如我在博客文章。
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导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!