Search code examples
pythongoogle-oauth

Get Google OAuth credentials using Python


Trying to get Get Google OAuth credentials, as mentioned in this sample:

google-oauthlib-tool --client-secrets c:\temp\client_secret_NNNN.json \
                 --credentials c:\temp\credentials.json \
                 --scope https://www.googleapis.com/auth/assistant-sdk-prototype \
                 --save

But calling this command there is no credentials.json file created. Any suggestions?

client_secret_NNNN.json:

{
    "installed": {
        "client_id": "NNNN.apps.googleusercontent.com",
        "project_id": "personal-stuff-54a54",
        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
        "token_uri": "https://accounts.google.com/o/oauth2/token",
        "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
        "redirect_uris": [
            "urn:ietf:wg:oauth:2.0:oob",
            "http://localhost"
        ]
    }
}

Log:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=633452378895-dg56psuf7er87nh1ehmoluekkkv0qj0j.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fassistant-sdk-prototype&state=6vqzd8wqETrkbsAwSVbvlrFKcJ4Vs8&access_type=offline
Traceback (most recent call last):
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\konopko\AppData\Local\Programs\Python\Python36-32\Scripts\google-oauthlib-tool.exe\__main__.py", line 9, in <module>
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\click\core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\google_auth_oauthlib\tool\__main__.py", line 104, in main
    creds = flow.run_local_server()
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\google_auth_oauthlib\flow.py", line 420, in run_local_server
    self.fetch_token(authorization_response=authorization_response)
  File "c:\users\konopko\appdata\local\programs\python\python36-32\lib\site-packages\google_auth_oauthlib\flow.py", line 239, in fetch_token
    kwargs.setdefault('client_secret', self.client_config['client_secret'])
KeyError: 'client_secret'

Solution

  • Your client_secret_NNNN.json should have a key client_secret, which is missing in your example. Go to https://console.developers.google.com/apis/credentials. You see a list of your OAuth2 client IDs. On the very right side of each client ID, there is a pen icon. Click it. On the page which now opens, you will see Client ID, Client secret, and Creation date. The Client secret should go into client_secret. The page also offers a download of the client secrets file, which then looks like this:

    {
        "installed": {
            "client_id": "your-client-id.apps.googleusercontent.com",
            "project_id": "your-project-id",
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://accounts.google.com/o/oauth2/token",
            "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
            "client_secret": "your-very-secret-token-here",
            "redirect_uris": [
                "urn:ietf:wg:oauth:2.0:oob",
                "http://localhost"
            ]
        }
    }