使用 oauth2.0 导入 Google 联系人 [英] Google contacts import using oauth2.0

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

问题描述

使用 python 和 oauth2.0 导入谷歌联系人的可能方法有哪些?

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

我们成功获得了凭据,我们的应用程序请求访问联系人,但在获得凭据后,我找不到发现联系人 api 的方法.

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

比如:

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

给我们 UnknownApiNameOrVersion 异常.看起来 Contacts API 不在 apiclient 支持的 API 列表中.

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

我正在寻找替代方法.

推荐答案

GoogleContacts API 不能与 google-api-python-client 库一起使用,因为它是一个 Google 数据 API,而 google-api-python-client 旨在与 基于发现的 API.

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.

无需经历 @NikolayFominyh 所描述的所有麻烦,您可以使用对 OAuth 2.0 的本机支持在 gdata-python-client 中.

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

要获取有效令牌,请按照 Google Developers 博文 对流程进行深入描述.

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

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)

然后,使用此令牌授权您的应用程序:

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 应用程序中,您可以简单地重定向,否则您需要在浏览器中访问该链接.

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.

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 中.

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

如果您在 Web 应用程序中,您将能够为路径 /oauth2callback 指定处理程序(例如),并且只需检索查询参数 code 将代码交换为令牌.例如,如果使用 WebOb:

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)

最后使用此令牌授权您的客户:

import gdata.contacts.service

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

更新(原始答案后 12 个月以上):

或者,您可以使用 google-api-python-client 支持,正如我在 博客文章.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆