`ValueError: need more than 1 value to unpack` in Python OAuthLib?

`ValueError: need more than 1 value to unpack` in Python OAuthLib?

我正在使用 OAuthLib 让访问者通过 LinkedIn 登录我的网站。我现在想 post 分享(更新)他们的个人资料 (as described here),我尝试使用以下代码来做到这一点:

xmlStr = '<share><comment>This is a comment.</comment><content><title>This is the title</title><description>This is the description</description><submitted-url>http://www.isittuesday.co.uk</submitted-url><submitted-image-url>http://stunningplaces.net/wp-content/uploads/2014/05/11-Rio-de-Janeiro-Cochabana-Beach.-Photo-by-ballnkicka.com_.jpg</submitted-image-url></content><visibility><code>connections-only</code></visibility></share>'
r = linkedInApp.post('people/~/shares', data=xmlStr, headers={'content-type': 'application/xml'})

不幸的是,这让我在第二行出现错误,说 ValueError: need more than 1 value to unpack(消息下方的回溯)。

有人知道怎么了吗?欢迎所有提示!

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/kramer65/repos/v/app/views.py", line 694, in authorized
    r = linkedInApp.post('people/~/shares', data=xmlStr, headers={'content-type': 'application/xml'})
  File "/Library/Python/2.7/site-packages/flask_oauthlib/client.py", line 371, in post
    return self.request(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/flask_oauthlib/client.py", line 424, in request
    data, content_type = encode_request_data(data, format)
  File "/Library/Python/2.7/site-packages/flask_oauthlib/client.py", line 154, in encode_request_data
    return url_encode(data or {}), 'application/x-www-form-urlencoded'
  File "/Library/Python/2.7/site-packages/werkzeug/urls.py", line 729, in url_encode
    return separator.join(_url_encode_impl(obj, charset, encode_keys, sort, key))
  File "/Library/Python/2.7/site-packages/werkzeug/urls.py", line 308, in _url_encode_impl
    for key, value in iterable:
ValueError: need more than 1 value to unpack

flask_oauthlib.client 模块中有一个奇怪的行为,如果您调用 request/get/post 方法而不使用 content_type 参数,它 url_encode 就是您的数据。由于您的 data 参数 (xmlStr) 是纯字符串,因此您会遇到异常。

最重要的是,将您的呼叫更改为以下内容,一切都应该没问题:

r = linkedInApp.post('people/~/shares', data=xmlStr, content_type='application/xml')