Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidRequestError #134

Closed
shoooe opened this issue Jul 21, 2014 · 3 comments
Closed

InvalidRequestError #134

shoooe opened this issue Jul 21, 2014 · 3 comments

Comments

@shoooe
Copy link

shoooe commented Jul 21, 2014

I get an InvalidRequestError while running this code:

from flask import Flask, request, redirect, session, url_for
from flask.json import jsonify
from requests_oauthlib import OAuth2Session
import os

app = Flask(__name__)

client_id = '...'
client_secret = '...'
redirect_uri = 'https://localhost:8100/callback'

authorization_base_url = "https://accounts.google.com/o/oauth2/auth"
token_url = "https://accounts.google.com/o/oauth2/token"
scope = [
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile"
]

@app.route("/")
def hello():
    google = OAuth2Session(client_id, scope=scope, redirect_uri=redirect_uri)
    authorization_url, state = google.authorization_url(authorization_base_url, access_type="offline", approval_prompt="force")
    session['oauth_state'] = state
    return redirect(authorization_url)

@app.route("/callback", methods=["GET"])
def callback():
    google = OAuth2Session(client_id, state=session['oauth_state'])
    token = google.fetch_token(token_url, client_secret=client_secret, authorization_response=request.url)
    session['oauth_token'] = token
    return redirect(url_for('.profile'))

@app.route("/profile", methods=["GET"])
def profile():
    google = OAuth2Session(client_id, token=session['oauth_token'])
    return jsonify(google.get('https://www.googleapis.com/oauth2/v1/userinfo').json())

if __name__ == "__main__":
    app.secret_key = os.urandom(24)
    app.run('0.0.0.0', debug=True, port=8100, ssl_context='adhoc')

Apparently it stops at the google.fetch_token(. I've manually check that the state is correct and it matches the given initial state. The token_url is correct.

Why is it not running? (HTTPS is enabled)

@shoooe
Copy link
Author

shoooe commented Jul 21, 2014

This is the stack trace:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/Jeffrey/py/index.py", line 29, in callback
    token = google.fetch_token(token_url, client_secret=client_secret, authorization_response=request.url)
  File "/Library/Python/2.7/site-packages/requests_oauthlib/oauth2_session.py", line 180, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "build/bdist.macosx-10.9-intel/egg/oauthlib/oauth2/rfc6749/clients/web_application.py", line 271, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "build/bdist.macosx-10.9-intel/egg/oauthlib/oauth2/rfc6749/parameters.py", line 303, in parse_token_response
    validate_token_parameters(params, scope)
  File "build/bdist.macosx-10.9-intel/egg/oauthlib/oauth2/rfc6749/parameters.py", line 310, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "build/bdist.macosx-10.9-intel/egg/oauthlib/oauth2/rfc6749/errors.py", line 239, in raise_from_error
    raise cls(**kwargs)
InvalidRequestError

@ib-lundgren
Copy link
Member

When supplying redirect_uri for the authorization/"hello" stage you need to supply it again in the token/"callback" stage.

Change (inside callback)

google = OAuth2Session(client_id, state=session['oauth_state'])

to

google = OAuth2Session(client_id, state=session['oauth_state'], redirect_uri=redirect_uri)

Using flasks debug screen you can look into the error message in more detail by printing "params" which reveals

 {u'error_description': u'Missing parameter: redirect_uri', u'error': u'invalid_request'}

This error is easy to miss and should probably be made clear in docs / examples.

@shoooe
Copy link
Author

shoooe commented Jul 22, 2014

Oh ok. This was silly. I have played with the wrong half of the stack trace.

@shoooe shoooe closed this as completed Jul 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants