question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Missing access Token parameter with OAuth2Session, but plain ol' requests works.

See original GitHub issue

I’m building an api integration, and I can get it working using plain requests yet not with requests-oauthlib. The traceback goes to the oauthlib itself, but this is similar to unsolved issue 286 on this repo, so I’ll post here for now.

The api I’m hitting isn’t public, so I’ll do my best to provide context even if you can’t directly reproduce.

Here’s what works with requests. This is at the callback stage, and the state is definitely the same state from the authorization url:

access_url = access_token_url + '?grant_type=authorization_code' + '&code=' + request.GET.get('code', '') + '&client_id=' + my_client_id + '&redirect_uri=' + redirect_uri + '&client_secret=' + client_secret + '&state=' + request.session['oauth_state']
token = requests.post(access_url)

Here’s what fails with requests-oauthlib:

myobject = OAuth2Session(client_id = client_id, redirect_uri = redirect_uri, state = request.session['oauth_state'])
token = myobject.fetch_token(access_token_url, authorization_response=request.build_absolute_uri(), client_secret=client_secret

I’m fairly sure that the request.build_absolute_uri() is not the problem, because that part works for other API integrations; that most certainly returns the full url it needs to parse.

Anyways here’s the error traceback:


File "/app/.heroku/python/lib/python3.6/site-packages/requests_oauthlib/oauth2_session.py" in fetch_token
  244.         self._client.parse_request_body_response(r.text, scope=self.scope)

File "/app/.heroku/python/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/clients/base.py" in parse_request_body_response
  411.         self.token = parse_token_response(body, scope=scope)

File "/app/.heroku/python/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py" in parse_token_response
  379.     validate_token_parameters(params)

File "/app/.heroku/python/lib/python3.6/site-packages/oauthlib/oauth2/rfc6749/parameters.py" in validate_token_parameters
  389.         raise MissingTokenError(description="Missing access token parameter.")

So, somehow oauthlib raises an error because it can’t find the access token parameter when it tries to validate whether parse_token_response() worked. So something seems to be going wrong at parse_token_response().

And this is what the token looks like when we do obtain the token:

{
"access_token": “<access_token>”,
"expires_in": 36000.0,
"refresh_token”: “<refresh_token>”
}

If someone can tell me how to inspect exactly what raw http requests are being sent by object.fetch_token(), that would also help me diagnose further. Is there a way to inspect the oauth2session object to find that, or does anyone happen to know an easy way to find that for a django app on heroku? (it’s not in heroku logs)

Thanks for contributing to such an elegant package. The overall quality really makes me want to fix this rather than use the plain old requests code in my app.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:11
  • Comments:24

github_iconTop GitHub Comments

12reactions
adamhoopercommented, Jun 11, 2019

I got this same experience with Intercom OAuth; I added include_client_id=True to my session.fetch_token() params and now it works.

9reactions
datacubedcommented, Aug 31, 2018

Has this been fixed at all? We use this package and it is excellent! But I’m having a similar issue to above.

client = LegacyApplicationClient(client_id=client_id) oauth = OAuth2Session(client=client) token = oauth.fetch_token( token_url=token_url, username=username, password=password, client_id=client_id, client_secret=client_secret)

This gives me the error:

Traceback (most recent call last): File “webgainstest.py”, line 26, in <module> client_secret=client_secret) File “/usr/local/lib/python2.7/dist-packages/requests_oauthlib/oauth2_session.py”, line 244, in fetch_token self._client.parse_request_body_response(r.text, scope=self.scope) File “/usr/local/lib/python2.7/dist-packages/oauthlib/oauth2/rfc6749/clients/base.py”, line 411, in parse_request_body_response self.token = parse_token_response(body, scope=scope) File “/usr/local/lib/python2.7/dist-packages/oauthlib/oauth2/rfc6749/parameters.py”, line 379, in parse_token_response validate_token_parameters(params) File “/usr/local/lib/python2.7/dist-packages/oauthlib/oauth2/rfc6749/parameters.py”, line 389, in validate_token_parameters raise MissingTokenError(description=“Missing access token parameter.”) oauthlib.oauth2.rfc6749.errors.MissingTokenError: (missing_token) Missing access token parameter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

flask - OAuth - Facebook MissingTokenError: (missing_token ...
I am using Flask_Dance extension to login using my facebook account, the app always returns an error that says: oauthlib.oauth2.rfc6749.errors.
Read more >
MissingTokenError when using us-west - Bugs & Features
MissingTokenError : (missing_token) Missing access token parameter ... oauth = OAuth2Session(client=client) token = oauth.fetch_token( ...
Read more >
requests-oauthlib 1.3.1 - PythonFix.com
OAuthlib authentication support for Requests. ... Missing access Token parameter with OAuth2Session, but plain ol' requests works.
Read more >
Request for authentication token fails with "Missing required ...
To make a clean test, we try to get authetication token using ... This command returns "Missing required parameter 'grant_type'" error.
Read more >
Refreshing tokens in OAuth 2 - Requests-OAuthlib
Commonly, only clients that authenticate may refresh tokens, e.g. web applications but not javascript clients. The provider will mention whether they allow ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found