本文介绍了用python-oauth2做文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一个Get请求是非常简单的:
$ $ $ $ c $ 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
但现在我们想用一个文件做一个POST。 (我们正在使用库python-oauth2)。建议?
解决方案
问题是,oauth不应该签署multipart / post数据,但它仍然需要签署其他参数。我的解决方法是使用python-oauth2签署非文件参数,然后使用urllib2手动发送请求。
这里有一个。见126-173行。
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?
解决方案
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.
这篇关于用python-oauth2做文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!