Doing a file upload with python-oauth2

2019-04-11 16:46发布

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, 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?

2条回答
ゆ 、 Hurt°
2楼-- · 2019-04-11 17:16

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.

查看更多
叛逆
3楼-- · 2019-04-11 17:31

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.

查看更多
登录 后发表回答