2014-04-09 91 views
2

我正在使用requests_oauthlib模塊進行OAuth身份驗證。 不幸的是,我不能重現您好!OAuth1流程acoording步驟之一,本教程:http://requests-oauthlib.readthedocs.org/en/latest/oauth1_workflow.html 如果我試圖獲得獲取響應它拋出我的跟隨着錯誤:OAuth1與requests_oauthlib產生URL編碼錯誤

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編碼這樣的網址:

#! /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呢?

感謝,

理查德

回答