【问题标题】:Unable to get OAuth "Request Token" while working with the Tumblr API using Python使用 Python 处理 Tumblr API 时无法获取 OAuth“请求令牌”
【发布时间】:2011-11-10 16:32:26
【问题描述】:

到目前为止,我一直在使用库来处理 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 返回 HTTP 错误 401:未授权,而不是 OAuth 请求令牌。

我尝试过但没有成功的事情:

  1. oauth_version 从“1.0”更改为“1.0a”,然后又改回来。
  2. OAuth 指南要求在consumer_secret 末尾添加“&”以获得oauth_key。我稍后尝试删除“&”,看看是否有任何不同。
  3. 检查 OAuth 参数是否已排序,并且已排序。
  4. 没有将字符串“Authorization:”添加到oauth_header,稍后再添加。两者都没有任何区别。

我哪里出错了?

【问题讨论】:

  • 你可能不应该把你的密钥放在这里
  • @Cal 这是专门为这个问题制作的虚拟应用程序。它就在 cmets 中:// Actual key and secret from a test app created using a **dummy** Tumblr account.

标签: python api oauth tumblr


【解决方案1】:

在上面的代码中只做 2 处简单的修改就解决了:

  1. normalized_http_method = 'GET' #not POST
  2. oauth_header = 'OAuth realm="http://www...' # “授权”这个词是不必要的。我早先在“我尝试过但没有成功的事情”中列出了这个,但是(1)中列出的错误让我偏离了轨道。解决了(1),我可以看到“授权”确实是不必要的。

当我最终做对时,Tumblr 发送给我的 OAuth 请求令牌: oauth_token=mbRUgyDkPePfkEztiLELMqUl1kyNXEcaTCCwpb7SoXDF9mhiTF&oauth_token_secret=5pXllXGKA8orAaUat1G7ckIfMfYup8juMBAgEELUkeMZoC3pv6&oauth_callback_confirmed=true

这是一次性令牌,为了完整起见,我在此处列出了它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 2017-02-27
    相关资源
    最近更新 更多