0

我正在尝试使用 oauth2 库访问保管箱帐户信息。我已经从 Dropbox 获得了访问令牌。然后我正在做的是:

parameters = {
    'oauth_consumer_key'    : DropboxConstants.app_key, #my app key
    'oauth_token'           : access_token_g,#token that i got [<key>,<secret>]
    'oauth_signature_method': oauth.SignatureMethod_HMAC_SHA1.name,
    'oauth_timestamp'       : oauth.generate_timestamp(),
    'oauth_nonce'           : oauth.generate_nonce(),
    'oauth_version'         : DropboxConstants.api_version,
    'oauth_signature'       : ''
}

#prepare signature
oauth_request=         oauth.Request(method="GET",url=DropboxConstants.account_info_url,parameters=parameters)
signature_method_m  = oauth.SignatureMethod_HMAC_SHA1()
signature           = signature_method_m.signing_base(consumer=consumer,request=oauth_request,token=access_token_g)

parameters['oauth_signature'] = signature[1]

#prepare url for accessing account info
url = "%s?oauth_token=%s&oauth_consumer_key=%s&oauth_signature_method=%s&oauth_timestamp=%s&oauth_nonce=%s&oauth_version=%s&oauth_signature=%s"%\
      (DropboxConstants.account_info_url,access_token['oauth_token'],parameters['oauth_consumer_key'],parameters['oauth_signature_method'],parameters['oauth_timestamp'],parameters['oauth_nonce'],parameters['oauth_version'], parameters['oauth_signature'])

return HttpResponseRedirect(url)

now the signature that is getting generated: GET&https%3A%2F%2Fapi.dropbox.com%2F0%2Faccount%2Finfo&oauth_consumer_key%3Dedw6k7d78hu8q8v%26oauth_nonce%3D39519001%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1314679561%26oauth_token%3Doauth_token_secret%253Dun58fgoc14n9jlv%2526oauth_token% 253D2ew2dafg0r40uwq%26oauth_version%3D1.0

我得到的错误是:

{"error": "无效签名。预期的签名基字符串:GET&https%3A%2F%2Fapi.dropbox.com%2F0%2Faccount%2Finfo&https%253A%252F%252Fapi.dropbox.com%252F0%252Faccount%252Finfo%3D% 26oauth_consumer_key%3Dedw6k7d78hu8q8v%26oauth_consumer_key%253Dedw6k7d78hu8q8v%2526oauth_nonce%253D39519001%2526oauth_signature_method%253DHMAC-SHA1%2526oauth_timestamp%253D1314679561%2526oauth_token%253Doauth_token_secret%25253Dun58fgoc14n9jlv%252526oauth_token%25253D2ew2dafg0r40uwq%2526oauth_version%253D1.0%3D%26oauth_nonce%3D39519001%26oauth_signature_method%3DHMAC-SHA1% 26oauth_timestamp%3D1314679561%26oauth_token%3D2ew2dafg0r40uwq%26oauth_version%3D1.0"}

4

2 回答 2

0

首先请使用 urlencode 正确转义字符:

from urllib import urlencode
...
parameters['oauth_token'] = access_token_g['oauth_token']
url = "?".join(DropboxConstants.account_info_url, urlencode(parameters))

看看这是否有帮助,如果没有,我会调查签名库

于 2011-08-30T06:48:51.040 回答
0

实际上,我已经通过代码中的一些更改解决了这个问题:

access_token_g = 
oauth.Token(key=access_token['oauth_token'],secret=access_token['oauth_token_secret'])
#prepare signature
oauth_request       = oauth.Request(method="GET",url=account_info_url,parameters=parameters)
signature_method_m  = oauth.SignatureMethod_HMAC_SHA1()

oauth_request.sign_request(signature_method=signature_method_m,consumer=consumer,token=access_token_g)

resp, content = client.request(oauth_request.to_url())

它给了我正确的内容..

于 2011-08-30T21:48:39.743 回答