在使用Python操作Tumblr API时无法获取OAuth“请求令牌”

5 投票
1 回答
4709 浏览
提问于 2025-04-17 01:29

我之前一直在用一些库来处理OAuth,但最近我想更深入地了解OAuth的基本过程。目前,我正在尝试用这段简单的代码通过OAuth 1.0a连接到Tumblr API v2

import urllib, urllib2, time, random, hmac, base64, hashlib

def makenonce():
    random_number = ''.join( str( random.randint( 0, 9 ) ) for _ in range( 40 ) )
    m = hashlib.md5( str( time.time() ) + str( random_number ) )
    return m.hexdigest()

def encodeparams(s):
    return urllib.quote( str( s ), safe='~' )

# Actual key and secret from a test app created using a dummy Tumblr account
consumer_key = '97oAujQhSaQNv4XDXzCjdZlOxwNyhobmDwmueJBCHWsFFsW7Ly'
consumer_secret = '5q1dpF659SOgSUb0Eo52aAyoud8N8QOuJu6enCG92aDR6WoMlf'

#oauth URLs
request_tokenURL = 'http://www.tumblr.com/oauth/request_token'

#oauth params
oauth_parameters = {
            'oauth_consumer_key'     : consumer_key,
            'oauth_nonce'            : makenonce(),
            'oauth_timestamp'        : str(int(time.time())),
            'oauth_signature_method' : "HMAC-SHA1",
            'oauth_version'          : "1.0"
            }

normalized_parameters = encodeparams( '&'.join( ['%s=%s' % ( encodeparams( str( k ) ), encodeparams( str( oauth_parameters[k] ) ) ) for k in sorted( oauth_parameters )] ) )
# Since I'm focusing only on getting the request token for now, I set this to POST.
normalized_http_method = 'POST'
normalized_http_url = encodeparams( request_tokenURL )
signature_base_string = '&'.join( [normalized_http_method, normalized_http_url, normalized_parameters] )
oauth_key = consumer_secret + '&'
hashed = hmac.new( oauth_key, signature_base_string, hashlib.sha1 )
oauth_parameters['oauth_signature'] = base64.b64encode( hashed.digest() )
oauth_header = 'Authorization: OAuth realm="http://www.tumblr.com",' + 'oauth_nonce="' + oauth_parameters['oauth_nonce'] + '",' + 'oauth_timestamp="' + oauth_parameters['oauth_timestamp'] + '",' + 'oauth_consumer_key="' + oauth_parameters['oauth_consumer_key'] + '",' + 'oauth_signature_method="HMAC-SHA1",oauth_version="1.0",oauth_signature="' + oauth_parameters['oauth_signature'] +'"'

# sample oauth_header generated by the code above:
# Authorization: OAuth realm="http://www.tumblr.com",oauth_nonce="c200a0e06f30b84b851ac3e99a71054b",oauth_timestamp="1315231855",oauth_consumer_key="97oAujQhSaQNv4XDXzCjdZlOxwNyhobmDwmueJBCHWsFFsW7Ly",oauth_signature_method="HMAC-SHA1",oauth_version="1.0",oauth_signature="kVAlmwolCX0WJIvTF9MB2UV5rnU="


req = urllib2.Request( request_tokenURL )
req.add_header( 'Authorization', oauth_header )
# If all goes well, Tumblr should send me the oauth request token.
print urllib2.urlopen( req ).read()

但是,Tumblr并没有返回我期待的OAuth请求令牌,而是给了我HTTP错误401:未授权

我尝试过的一些方法,但都没有成功:

  1. oauth_version从“1.0”改成“1.0a”,然后又改回去。
  2. 有一个关于OAuth的指南说在consumer_secret的末尾加一个'&',这样才能得到oauth_key。我后来试着把这个'&'去掉,看看会不会有变化。
  3. 检查了一下OAuth参数是否排序,结果是排序好的。
  4. 没有在oauth_header里加“Authorization: ”这个字符串,后来又加回去。结果也没有什么变化。

我哪里出错了呢?

1 个回答

4

我通过对上面的代码做了两个简单的修改,解决了问题:

  1. normalized_http_method = 'GET' # 不是 POST
  2. oauth_header = 'OAuth realm="http://www...' # 这里的“Authorization”这个词是多余的。我之前把它去掉了,正如我在“我尝试过但没有成功的事情”中列出的那样,但在解决了第一个问题后,我才发现“Authorization”确实是不必要的。

当我终于搞定的时候,Tumblr发给我的OAuth请求令牌是:

oauth_token=mbRUgyDkPePfkEztiLELMqUl1kyNXEcaTCCwpb7SoXDF9mhiTF&oauth_token_secret=5pXllXGKA8orAaUat1G7ckIfMfYup8juMBAgEELUkeMZoC3pv6&oauth_callback_confirmed=true

这是一个一次性的令牌,我在这里列出它只是为了完整性。

撰写回答