gpt4 book ai didi

python - 使用 Python-oauth2 在 python 中为 tumblr API 初始化 Oauth 客户端

转载 作者:太空狗 更新时间:2023-10-29 21:20:19 24 4
gpt4 key购买 nike

我是 Oauth 的新手。在过去,对于用 Python 编写的 twitter 应用程序,我使用 python-oauth2 库来初始化客户端,如下所示:

consumer = oauth.Consumer(key = CONSUMER_KEY, secret = CONSUMER_SECRET)
token = oauth.Token(key = ACCESS_KEY, secret = ACCESS_SECRET)
client = oauth.Client(consumer, token)

这很容易,因为 twitter 提供了 CONSUMER 和 ACCESS key 和 secret 。但现在我需要为 tumblr 做同样的事情。问题是 tumblr 只提供 CONSUMER_KEY、CONSUMER_SECRET 和这些 url:

Request-token URL   http://www.tumblr.com/oauth/request_token
Authorize URL http://www.tumblr.com/oauth/authorize
Access-token URL http://www.tumblr.com/oauth/access_token

如何使用这些数据初始化客户端以访问 tumblr API?

更新

jterrace 建议了我以前尝试使用的代码。它的问题是 oauth_callback。如果我没有指定任何内容,api 将返回错误“未指定 oauth_callback”,但如果我确实指定了一些 url,如“http://example.com/oauthcb/”并点击链接 http://www.tumblr.com/oauth/authorize?oauth_token=9ygTF ...,然后按“允许”按钮,tumblr 不显示任何 PIN 代码页,它会立即重定向到那个回调 url,这是无用的,因为它是桌面应用程序。为什么不显示 PIN 码?

更新 2

Tumblr API 不支持 PIN 码授权。改为使用 xAuth - https://groups.google.com/group/tumblr-api/browse_thread/thread/857285e6a2b4268/15060607dc306c1d?lnk=gst&q=pin#15060607dc306c1d

最佳答案

首先,导入 oauth2 module并设置服务的 URL 和消费者信息:

import oauth2

REQUEST_TOKEN_URL = 'http://www.tumblr.com/oauth/request_token'
AUTHORIZATION_URL = 'http://www.tumblr.com/oauth/authorize'
ACCESS_TOKEN_URL = 'http://www.tumblr.com/oauth/access_token'
CONSUMER_KEY = 'your_consumer_key'
CONSUMER_SECRET = 'your_consumer_secret'

consumer = oauth2.Consumer(CONSUMER_KEY, CONSUMER_SECRET)
client = oauth2.Client(consumer)

第 1 步:获取请求 token 。这是一个临时 token ,用于让用户授权访问 token 并签署请求以获取说访问 token 。

resp, content = client.request(REQUEST_TOKEN_URL, "GET")

request_token = dict(urlparse.parse_qsl(content))
print "Request Token:"
print " - oauth_token = %s" % request_token['oauth_token']
print " - oauth_token_secret = %s" % request_token['oauth_token_secret']

第 2 步:重定向到提供商。因为这是一个 CLI 脚本,我们不重定向。在 Web 应用程序中,您会将用户重定向到 URL下面。

print "Go to the following link in your browser:"
print "%s?oauth_token=%s" % (AUTHORIZATION_URL, request_token['oauth_token'])

# After the user has granted access to you, the consumer, the provider will
# redirect you to whatever URL you have told them to redirect to. You can
# usually define this in the oauth_callback argument as well.
oauth_verifier = raw_input('What is the PIN? ')

第 3 步:一旦消费者将用户重定向回 oauth_callback您可以请求用户已批准的访问 token 的 URL。你使用请求 token 来签署此请求。完成后你扔掉请求 token 并使用返回的访问 token 。你应该存储这个访问 token 在安全的地方,例如数据库,以供将来使用。

token = oauth2.Token(request_token['oauth_token'], request_token['oauth_token_secret'])
token.set_verifier(oauth_verifier)
client = oauth2.Client(consumer, token)

resp, content = client.request(ACCESS_TOKEN_URL, "POST")
access_token = dict(urlparse.parse_qsl(content))

print "Access Token:"
print " - oauth_token = %s" % access_token['oauth_token']
print " - oauth_token_secret = %s" % access_token['oauth_token_secret']
print

现在您有了访问 token ,您可以使用它调用 protected 方法。

编辑:结果是 tumblr 不支持 PIN 授权方法。相关帖子here .

关于python - 使用 Python-oauth2 在 python 中为 tumblr API 初始化 Oauth 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7569018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com