开发者

Doing a file upload with python-oauth2

开发者 https://www.devze.com 2023-03-25 02:36 出处:网络
A Get request is pretty easy: def build_request(url, method=\'GET\'): params = { \'oauth_version\': \"1.0\",

A Get request is pretty easy:

def build_request(url, method='GET'):
    params = {                                            
        'oauth_version': "1.0",
        'oauth_nonce': oauth2.generate_nonce(),
        'oauth_timestamp': int(time.time())
    }
    consumer = oauth2.Consumer(key='****',secret='******')
    params['oauth_consumer_key'] = consumer.key

    req = oauth2.Request(method=method, 开发者_StackOverflow社区url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, None)
    return req

But now, we want to make a POST with a file. (We're using the library python-oauth2). Suggestions?


The problem is that oauth is not supposed to sign multipart/post data, but it still needs to sign the other parameters. The way I got around it was to use python-oauth2 to sign the non-file parameters and then send the request manually with urllib2.

Here's an example script. See lines 126 - 173.


From reading the source it appears that Request takes a method to state with HTTP request to use.

Simply change your req to

req = oauth2.Request(method='POST', url=url, parameters=params)

https://github.com/simplegeo/python-oauth2/blob/master/oauth2/init.py#L342 for more info

That might go part way to solving your issue, as for the file upload you might be aable to work with supplying headers with the content see:

https://github.com/simplegeo/python-oauth2/blob/master/oauth2/init.py#L646

Apologies I have not had chance to test this yet.

0

精彩评论

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