Python 使用oauth2.0导入Google联系人
使用python和oauth2.0导入google联系人的可能方法有哪些 我们成功地获得了凭据,我们的应用程序请求访问联系人,但在获得凭据后,我找不到发现联系人api的方法 比如:Python 使用oauth2.0导入Google联系人,python,google-api,oauth-2.0,contacts,google-api-python-client,Python,Google Api,Oauth 2.0,Contacts,Google Api Python Client,使用python和oauth2.0导入google联系人的可能方法有哪些 我们成功地获得了凭据,我们的应用程序请求访问联系人,但在获得凭据后,我找不到发现联系人api的方法 比如: from apiclient.discover import build import httplib2 http = httplib2.Http() #Authorization service = build("contacts", "v3", http=http) 给我们提供了未知的Napinam
from apiclient.discover import build
import httplib2
http = httplib2.Http()
#Authorization
service = build("contacts", "v3", http=http)
给我们提供了未知的NapinameorVersion
异常。
似乎Contacts API不在ApicClient支持的API列表中
我在寻找其他的方法 最终的解决方案相对容易 步骤1 获取oauth2.0令牌。在官方文件中有很好的记录: 步骤2 现在我们有了令牌,但无法发现联系人API。 但是您可以发现,在oauth2.0中,您可以导入联系人。 您可以发现,您在凭据中拥有在步骤1中获得的访问令牌。 要访问contacts api,必须将以下参数添加到头中:“授权”:“OAuth%s”%access\u令牌 步骤3 现在您必须传递到GoogleLibrary令牌,它将与oauth1.0令牌兼容。 可通过以下代码执行此操作:
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()
无法与谷歌api python客户端
库一起使用,因为它是一个,而谷歌api python客户端
旨在与一起使用
您可以在gdatapythonclient
中使用对OAuth 2.0的本机支持,而不必经历所描述的所有问题
要获得一个有效的令牌,请按照Google开发者提供的说明对该过程进行深入描述
首先,创建一个令牌对象:
然后,使用此令牌授权您的应用程序:
生成此authorize_url
后,您(或应用程序的用户)需要访问它并接受OAuth 2.0提示。如果这是在web应用程序中,您可以简单地重定向,否则您将需要访问浏览器中的链接
授权后,将代码交换为令牌:
如果您访问了浏览器,则需要将重定向到的URL复制到变量redirect\u URL
在web应用程序中,您可以指定路径/oauth2callback
(例如)的处理程序,并且只需检索查询参数code
,即可将代码交换为令牌。例如,如果使用WebOb
:
redirect_url = atom.http_core.Uri.parse_uri(self.request.uri)
最后,使用此令牌授权您的客户:
更新(原始答案后12个月以上):
或者,您可以使用
谷歌api python客户端
支持,正如我在一篇文章中所描述的。太棒了!当我问这个问题的时候,这就是我想要的!在重定向处理程序视图“oauth2callback”中,我无法访问在初始视图中创建的auth_令牌对象。你知道如何做到这一点吗?
APPLICATION_REDIRECT_URI = 'http://www.example.com/oauth2callback'
authorize_url = auth_token.generate_authorize_url(
redirect_uri=APPLICATION_REDIRECT_URI)
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)
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)