在Django中使用python的oauth2库来访问dropbox

时间:2011-08-30 04:59:56

标签: django oauth dropbox-api

我正在尝试使用oauth2库访问dropbox帐户信息。 我从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)

现在生成的签名: 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&amp; https%3A%2F%2Fapi.dropbox.com%2F0%2Faccount%2Finfo&amp; 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" }

2 个答案:

答案 0 :(得分:0)

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

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

看看这是否有帮助,如果不是,我会调查签名基础

答案 1 :(得分: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())

它给了我正确的内容..