2012-04-20 29 views
0

我是新来的使用任何API,以及HTTP请求,所以我有一些麻烦。我不知道如何将令牌信息传递给API,在我从GET请求中获取它之后。 imgur API表示它需要三个端点:http://api.imgur.com/auth但我只能得到第二个端点,因为我无法传递请求的令牌。追溯传递令牌信息后,从Imgur API使用请求接收它-oauth

模块文档对我来说非常含糊: 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 

什么是最好的继续?

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

的Imgur API建议我看一些Twitter的Docs软件以便得到一个好主意,但一个我读:http://net.tutsplus.com/tutorials/php/how-to-authenticate-users-with-twitter-oauth/,是有点过我的头,因为它是PHP,但现在看来似乎是我应该正在做。

回答

1

我是requests-oauth的作者。我最近发布了此应用的0.4.0版本,并改进了帮助新成员加入OAuth的文档。

这里,希望是回答你的问题: https://github.com/maraujop/requests-oauth#3-legged-authorization

对不起,这么长的时间来回答。

+0

太好了,谢谢!请注意其他人,确保您在尝试解决方案之前将请求-auth正确更新为'0.4.0'! – TankorSmash 2012-05-21 19:31:06

相关问题