2016-11-20 90 views
0

一直遵循有關使用RequestsBeautifulSoup4進行網頁抓取的教程。我正在嘗試訪問www.showmyhomework.com網站的信息,但我相信該網站正在使用OAuth2授權。不幸的是,本教程不包括如何使用OAuth2來完成此操作。花了幾個小時閱讀OAuth2文檔,但不知道如何讓我的Python腳本請求令牌並訪問網頁。下面的python腳本來自OAuth2文檔,但我顯然做錯了!使用requests_oauthlib訪問OAuth2網頁

from oauthlib.oauth2 import LegacyApplicationClient 
from requests_oauthlib import OAuth2Session 

client_Id = 'My_Google_supplied_OAuth2_Client_Id.apps.googleusercontent.com' 
client_Secret = 'My_Google_supplied OAuth Client_Secret' 
redirect_uri = 'https://your.callback/uri' 

oauth = OAuth2Session(client=LegacyApplicationClient(client_id=client_Id)) 
token = oauth.fetch_token(token_url='https://www.showmyhomework.com/oauth2/token', 
      username="My_Username", password="My_Password", client_id=client_Id, 
      client_secret=client_Secret) 
print(token) 

我響應得到這些錯誤:

=================== RESTART: E:/Programs/Python27/Auth.py =================== 

Traceback (most recent call last): 
    File "E:/Programs/Python27/Auth.py", line 11, in <module> 
    client_secret=client_Secret) 
    File "E:\Programs\Python27\lib\site-packages\requests_oauthlib\oauth2_session.py", line 244, in fetch_token 
    self._client.parse_request_body_response(r.text, scope=self.scope) 
    File "E:\Programs\Python27\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 409, in parse_request_body_response 
    self.token = parse_token_response(body, scope=scope) 
    File "E:\Programs\Python27\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 376, in parse_token_response 
    validate_token_parameters(params) 
    File "E:\Programs\Python27\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 386, in validate_token_parameters 
    raise MissingTokenError(description="Missing access token parameter.") 
MissingTokenError: (missing_token) Missing access token parameter. 

回答

0

我有同樣的問題,我在fetch_token加入auth=False解決它。

oauth = OAuth2Session(client=LegacyApplicationClient(client_id=client_Id)) 
token = oauth.fetch_token(token_url='https://www.showmyhomework.com/oauth2/token', 
     username="My_Username", password="My_Password", client_id=client_Id, 
     client_secret=client_Secret, auth=False) 

祝你好運。

+0

爲什麼我們需要設置此標誌開始? –