blueFast
blueFast

Reputation: 44501

Google OAuth2 command-line example

Google has an example of an OAuth2 client here

I am completelly new to OAuth2 and I would like to get this example working before I move to integrate OAuth2 with my application. What I have done is the following:

  1. Register a test application
  2. Get Client ID and Client Secret
  3. Configure those values into client_secrets.json
  4. Run the test app: python moderator.py

The application opens up a browser, where I can (as a user) authorize the application to access my account. But Google is complaining like this (400 Bad Request):

Error: redirect_uri_mismatch
The redirect URI in the request: http://localhost:8080/ did not match a registered redirect URI
Learn more
Request Details
from_login=1
scope=https://www.googleapis.com/auth/moderator
response_type=code
access_type=offline
redirect_uri=http://localhost:8080/
approval_prompt=auto
as=-xxxxxxxxxxxxx
pli=1
client_id=xxxxxxxxxxx.apps.googleusercontent.com
authuser=0
hl=en

I guess the localhost:8080 is coming from an internal web server started by moderator.py. My question is: has somebody goten this example to work? What other components do I need (apache configuration, DNS, ...)

I am very confused with OAuth2 and any help would be greatly appreciated.

Upvotes: 8

Views: 6465

Answers (4)

GoTop
GoTop

Reputation: 913

Your redirect_uri is set to 'http://localhost:8080/' because you pass a default(I don't know how to describe it) flags parameter to run_flow(flow, storage, flags)

if you look at the define for run_flow() function you will find this:

It presumes it is run from a command-line application and supports the
following flags:

    ``--auth_host_name`` (string, default: ``localhost``)
       Host name to use when running a local web server to handle
       redirects during OAuth authorization.

    ``--auth_host_port`` (integer, default: ``[8080, 8090]``)
       Port to use when running a local web server to handle redirects
       during OAuth authorization. Repeat this option to specify a list
       of values.

    ``--[no]auth_local_webserver`` (boolean, default: ``True``)
       Run a local web server to handle redirects during OAuth
       authorization.

Upvotes: 0

Fullplate
Fullplate

Reputation: 69

Perhaps try registering your external IP with Google (may require some port fowarding on your router)? If this fails, maybe you could use Python's SimpleServer, register your IP and get this server to handle the redirect.

Upvotes: 0

GabrielF
GabrielF

Reputation: 2121

First of all, sorry if my answer isn't very precise, because I'm also very new to OAuth (and even python)... and also sorry if it came too late, I don't usually access here.

Have you tried using this (worked for me): REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

Check this: https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi

Here I have a piece of code with a complete OAuth flow working.

Upvotes: 2

Colin Dunklau
Colin Dunklau

Reputation: 3111

In OAuth 2.0, the redirect_uri parameter is usually registered with the provider. The provider should also be enforcing https-only redirect_uri.

You need to register the redirect_uri with Google here: https://code.google.com/apis/console/?pli=1#access

Upvotes: 0

Related Questions