【问题标题】:Doing a file upload with python-oauth2使用 python-oauth2 进行文件上传
【发布时间】:2011-10-18 23:15:10
【问题描述】:

Get 请求非常简单:

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)。有什么建议吗?

【问题讨论】:

标签: python file-upload oauth


【解决方案1】:

问题是 oauth 不应该对 multipart/post 数据进行签名,但它仍然需要对其他参数进行签名。我解决它的方法是使用 python-oauth2 对非文件参数进行签名,然后使用 urllib2 手动发送请求。

这是example script。请参见第 126 - 173 行。

【讨论】:

    【解决方案2】:

    从阅读源代码看来,Request 采用了一种方法来使用 HTTP 请求声明。

    只需将您的请求更改为

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

    https://github.com/simplegeo/python-oauth2/blob/master/oauth2/init.py#L342 了解更多信息

    这可能会部分解决您的问题,至于文件上传,您可能可以使用内容提供标题:

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

    抱歉,我还没有机会对此进行测试。

    【讨论】:

      猜你喜欢
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      相关资源
      最近更新 更多