使用 gdata.contacts.client 和 oauth2 检索联系人 [英] Retrieving contacts with gdata.contacts.client and oauth2

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

问题描述

我正在使用 oAuth2WebServerFlow 来获取 oAuth 访问令牌,然后检索用户的联系人列表.我使用 web2py 作为网络框架.

I am using oAuth2WebServerFlow to get an oAuth access token and then retrieve a list of a user's contacts. I'm using web2py as the web framework.

    flow = oauth2client.client.OAuth2WebServerFlow(client_id=CLIENT_ID,
                                                   client_secret=CLIENT_SECRET,
                                                  scope='https://www.google.com/m8/feeds', 
                                                   user_agent=USER_AGENT)
    callback = 'http://127.0.0.1:8000/Test/searcher/oauth2callback'
    authorise_url = flow.step1_get_authorize_url(callback)
    session.flow = pickle.dumps(flow)
    redirect(authorise_url) 

使用重定向然后按如下方式处理

With the redirect then being handled as follows

flow = pickle.loads(session.flow)
credentials = flow.step2_exchange(request.vars) 

我的问题是如何将上面返回的 OAuth2Credentials 对象更改为 OAuth2AccessToken 对象,然后我可以使用它来授权对联系人库的请求,例如:

My question is how to change the OAuth2Credentials object returned above into an OAuth2AccessToken object, that I can then use to authorise a request to the contacts library with something like:

gc = gdata.contacts.client.ContactsClient(source="")
token.authorize(gc)
gc.GetContacts

我尝试了各种方法都没有成功,通常会收到无效授权"的 oAuth2AccessTokenError 消息.我在想这样的事情可能会奏效,但也认为必须有一种更简单的方法!

I've tried various methods with no success, normally getting an oAuth2AccessTokenError message of "Invalid Grant". I'm thinking something like this may work but also think there must be a simpler way!

token = gdata.gauth.OAuth2Token(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, scope='https://www.google.com/m8/feeds', user_agent=USER_AGENT)

    token.redirect_uri = 'http://127.0.0.1:8000/Test/searcher/oauth2callback'
    token.get_access_token(<<code to pass the access_token out of the Credentials object??>>)

有人可以帮忙吗?

推荐答案

我设法让这个工作.这实际上非常简单,我只是停止使用 OAuth2WebServerFlow,无论如何它似乎并没有增加太多价值.所以新代码看起来像这样:

I managed to get this working. It was pretty straightforward actually, I just stopped using the OAuth2WebServerFlow, which didn't seem to be adding much value anyway. So the new code looks like this:

token = gdata.gauth.OAuth2Token(client_id, client_secret, scope, ua)
session.token = pickle.dumps(token)                   
redirect(token.generate_authorize_url(redirect_uri='http://127.0.0.1:8000/Test/default/oauth2callback'))

关注

def oauth2callback():
    token = pickle.loads(session.token)
    token.redirect_uri='http://127.0.0.1:8000/Test/default/oauth2callback'
    token.get_access_token(request.vars.code)
    gc = gdata.contacts.client.ContactsClient(source='')
    gc = token.authorize(gc)
    feed = gc.GetContacts()

希望这对某人有帮助!

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

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