追溯传递令牌信息,在使用请求 oauth 从 Imgur API 接收后



我是使用任何API以及HTTP请求的新手,所以我遇到了一些麻烦。我不确定如何在从 GET 请求获取令牌信息后将令牌信息传递给 API。imgur API 说它需要三个端点:http://api.imgur.com/auth 但我只能到达第二个端点,因为我无法传递请求的令牌。

模块文档对我来说非常模糊:https://github.com/maraujop/requests-oauth

这是我编写的代码,它应该成功通过身份验证,但它返回一个 html 页面 http://pastebin.com/19hnBy1C。

import requests
from oauth_hook import OAuthHook
import cgi
OAuthHook.consumer_key = 'XXXXXXX'
OAuthHook.consumer_secret = 'YYYYY'

#just in case
oauth_hook = OAuthHook(header_auth=True)
#request the tokens, using the keys set above
r = requests.get(url='https://api.imgur.com/oauth/request_token', hooks={'pre_request': oauth_hook,})
#parse the lsit
tokenList = cgi.parse_qs(r.content)
token = tokenList['oauth_token']
tokenSecret = tokenList['oauth_token_secret']
#this is where I'm not sure what to do, 
#I create a new hook with the tokens I received
oauth_hook = OAuthHook(access_token=token[0], access_token_secret=tokenSecret[0])
#send the GET request
r = requests.get(url='https://api.imgur.com/oauth/authorize', hooks={'pre_request': oauth_hook,})
#this is that HTML that requires me to enter account info, how do I do that in python?
print r.content
#this is the next step, which, if you uncomment the last print, shows that the auth failed.
r = requests.get(url='https://api.imgur.com/oauth/access_token', hooks={'pre_request': oauth_hook,})
#print r.text

继续的最佳方式是什么?

我想也许我可以向authorize api 发送 POST,将我的用户名/密码作为数据或参数,但这似乎不起作用。

Imgur API 建议我查看一些 Twitter 文档以获得一个好主意,但我正在阅读的那个:http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/,有点超出我的头脑,因为它是 PHP,尽管它似乎是我应该做的。

我是 requests-oauth 的作者。我最近发布了此应用程序的 0.4.0 版,并改进了文档以帮助 OAuth 的新人。

希望这是您问题的答案:https://github.com/maraujop/requests-oauth#3-legged-authorization

很抱歉花了这么长时间来回答。

最新更新