带有requests_oauthlib的OAuth1会产生URL编码错误

时间:2014-04-09 14:18:45

标签: python oauth urlencode

我正在使用requests_oauthlib模块进行OAuth身份验证。 遗憾的是,根据本教程中的第一步,我无法重现OAuth1工作流程:http://requests-oauthlib.readthedocs.org/en/latest/oauth1_workflow.html 如果我尝试获取获取响应,则会抛出以下错误:

Traceback (most recent call last):
  File "/home/neumannr/test.py", line 18, in <module>
    fetch_response = oauth.fetch_request_token(request_token_url)
  File "/usr/lib/python2.7/site-packages/requests_oauthlib/oauth1_session.py", line 195, in fetch_request_token
    token = self._fetch_token(url)
  File "/usr/lib/python2.7/site-packages/requests_oauthlib/oauth1_session.py", line 264, in _fetch_token
    token = dict(urldecode(self.post(url).text))
  File "/usr/lib/python2.7/site-packages/oauthlib/common.py", line 135, in urldecode
    raise ValueError('Not a valid urlencoded string.')
ValueError: Not a valid urlencoded string.

我的测试代码如下:

#! /usr/bin/env python


# Using OAuth1Session
from requests_oauthlib import OAuth1Session

# Using OAuth1 auth helper
import requests
from requests_oauthlib import OAuth1

client_key = 'a'
client_secret = 'b'
request_token_url = 'https://api.twitter.com/oauth/request_token'


# Using OAuth1Session
oauth = OAuth1Session(client_key, client_secret=client_secret)
fetch_response = oauth.fetch_request_token(request_token_url)

由于URL也没有在教程中进行URL编码,我不明白为什么会这样。如果我尝试对URL进行URL编码,请执行以下操作:

#! /usr/bin/env python


# Using OAuth1Session
import urllib
from requests_oauthlib import OAuth1Session

# Using OAuth1 auth helper
import requests
from requests_oauthlib import OAuth1

client_key = 'a'
client_secret = 'b'
request_token_url = urllib.quote('https://api.twitter.com/oauth/request_token')


# Using OAuth1Session
oauth = OAuth1Session(client_key, client_secret=client_secret)
fetch_response = oauth.fetch_request_token(request_token_url)

我得到了一个缺少的架构错误:

Traceback (most recent call last):
  File "/home/neumannr/test.py", line 19, in <module>
    fetch_response = oauth.fetch_request_token(request_token_url)
  File "/usr/lib/python2.7/site-packages/requests_oauthlib/oauth1_session.py", line 195, in fetch_request_token
    token = self._fetch_token(url)
  File "/usr/lib/python2.7/site-packages/requests_oauthlib/oauth1_session.py", line 264, in _fetch_token
    token = dict(urldecode(self.post(url).text))
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 377, in post
    return self.request('POST', url, data=data, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 324, in request
    prep = req.prepare()
  File "/usr/lib/python2.7/site-packages/requests/models.py", line 222, in prepare
    p.prepare_url(self.url, self.params)
  File "/usr/lib/python2.7/site-packages/requests/models.py", line 291, in prepare_url
    raise MissingSchema("Invalid URL %r: No schema supplied" % url)
requests.exceptions.MissingSchema: Invalid URL u'https%3A//api.twitter.com/oauth/request_token': No schema supplied

我如何使用oauthlib_requests?

谢谢,

理查德

1 个答案:

答案 0 :(得分:0)

错误可能是尝试解码响应失败的结果。有关详细信息,请参阅OAuthlib issue 124