开发者

using oauth2 library of python in Django to access dropbox

I am trying to access dropbox account info using oauth2 library. I have got hold of access token from dropbox. Then what I am doing is:

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'         : DropboxConstan开发者_JAVA技巧ts.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 I get is:

{"error": "Invalid signature. Expected signature base string: 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"}


firstly please use urlencode to escape chars correctly:

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

see if this helps, if not i'll look into the signiature base


Actually i have solved this problem with a bit change in code as:

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())

It gives me correct content..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜