使用python-oauth2在tumblr API中的python中的Oauth客户端初始化 [英] Oauth client initialization in python for tumblr API using Python-oauth2

查看:167
本文介绍了使用python-oauth2在tumblr API中的python中的Oauth客户端初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to Oauth. In the past for twitter applications written in Python i used python-oauth2 library to initialize client like this:

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密钥和机密.但是现在我需要为tumblr做同样的事情.问题在于,tumblr仅提供CONSUMER_KEY,CONSUMER_SECRET和以下网址:

That was easy because twitter provides both CONSUMER and ACCESS keys and secrets. But now i need to do the same for tumblr. The problem is that tumblr provides only CONSUMER_KEY, CONSUMER_SECRET and these urls:

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?

Using this data how can i initialize client to access tumblr API?

UPD

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

jterrace suggested a code i tried to use before. The problem with it is oauth_callback. If i don't specify any, api returns error "No oauth_callback specified", but if i do specify some url like "http://example.com/oauthcb/" and follow the link http://www.tumblr.com/oauth/authorize?oauth_token=9ygTF..., then press Allow button, tumblr doesn't show any PIN code page, it immediately redirects to that callback url, which is useless since it's desktop application. Why PIN code isn't shown?

UPD 2

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

Tumblr API doesn't support PIN code authorization. Use xAuth instead - https://groups.google.com/group/tumblr-api/browse_thread/thread/857285e6a2b4268/15060607dc306c1d?lnk=gst&q=pin#15060607dc306c1d

推荐答案

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

First, import the oauth2 module and set up the service's URL and consumer information:

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步:获取请求令牌.这是一个临时令牌,用于 让用户授权访问令牌并签署请求以获取 表示访问令牌.

Step 1: Get a request token. This is a temporary token that is used for having the user authorize an access token and to sign the request to obtain said access 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 在下面.

Step 2: Redirect to the provider. Since this is a CLI script we do not redirect. In a web application you would redirect the user to the URL below.

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 您可以请求用户批准的访问令牌的URL.您使用 请求令牌以签署此请求.完成此操作后,您将丢弃 请求令牌并使用返回的访问令牌.你应该储存这个 在安全的地方(例如数据库)访问令牌以供将来使用.

Step 3: Once the consumer has redirected the user back to the oauth_callback URL you can request the access token the user has approved. You use the request token to sign this request. After this is done you throw away the request token and use the access token returned. You should store this access token somewhere safe, like a database, for future use.

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

现在有了访问令牌,您可以使用它来访问受保护的方法.

Now that you have an access token, you can call protected methods with it.

编辑:事实证明tumblr不支持PIN授权方法.相关帖子此处.

Turns out that tumblr does not support the PIN authorization method. Relevant post here.

这篇关于使用python-oauth2在tumblr API中的python中的Oauth客户端初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆