2012-04-20 30 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

相關問題