我不熟悉使用任何 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,尽管它似乎是我应该做的。