【问题标题】:Retroactively passing token information, after receiving it from the Imgur API using requests-oauth在使用 requests-oauth 从 Imgur API 接收令牌信息后,追溯传递令牌信息
【发布时间】:2012-05-02 07:55:14
【问题描述】:

我不熟悉使用任何 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,但这似乎不起作用。

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

【问题讨论】:

    标签: python oauth imgur


    【解决方案1】:

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

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

    抱歉这么久才回答。

    【讨论】:

    • 太好了,谢谢!请注意,在尝试他的解决方案之前,请确保您将 requests-oauth 正确更新为 0.4.0
    猜你喜欢
    • 2015-04-28
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多