[Django]-How to authorize an app with Google in Django on a live site

5👍

Explanation:

The example that Google provides on that page should actually work in a live environment. The area that I believe you are getting stuck on is the credential file that Google tells you to download after you enable the API.

Assuming you’re using the credential file that can be downloaded after the API is enabled, the credential file will look like this:

{
  "installed": {
    "client_id": "<CLIENT_ID>",
    "project_id": "<PROJECT_ID>",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "<CLIENT_SECRET>",
    "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob", "http://localhost"]
  }
}

The installed property is telling your program that this is an installed app on a local environment which isn’t what you want.


Solution:

  1. Go into your project’s credentials
  2. Click, "Create Credentials" and then click "OAuth client ID"
  3. On the next page, select "Web application" and add your redirect URIs (i.e. the url that you want Google to take the user after they have been authenticated)
  4. Click "Create"
  5. You’ll be taken back to the main credentials page and you’ll see your new credentials under "OAuth 2.0 client IDs". Click the link of the newly created credentials.
  6. Now that you’re in the settings of your credentials page, you can now click, "DOWNLOAD JSON"

Assuming everything worked as expected, you should now have a json file that contains something like this:

{
  "web": {
    "client_id": "<CLIENT_ID>",
    "project_id": "<PROJECT_ID>",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "<CLIENT_SECRET>",
    "redirect_uris": [
      "<YOUR_REDIRECT_URIS>"
    ]
  }
}

Leave a comment