Colin M
Colin M

Reputation: 23

Using requests_oauthlib to access OAuth2 web page

Have been following a tutorial about using Requests and BeautifulSoup4 for web scraping. I am trying to access info from www.showmyhomework.com website but I believe the site is using OAuth2 authorisation. Unfortunately the tutorial does not cover how to do this with pages with OAuth2. Have spent hours reading the OAuth2 documentation but can't figure out how I get my Python script to request a token and get access to the web page. The python script below is from the OAuth2 documentation but I am clearly doing something wrong!

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)

I get these errors in response:

=================== 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. 

Upvotes: 2

Views: 4119

Answers (1)

black_rez
black_rez

Reputation: 82

I have the same issue and I resolved it by adding auth=False in fetch_token.

 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)

Good luck.

Upvotes: 2

Related Questions