Skip to main content

A library to help integrate Google OAuth 2.0 to your Django application.

Project description

Google APIs OAuth Django

PyPI version Downloads

A library to help integrate Google OAuth 2.0 to your Django application. This library retrieves the necessary tokens you need to access the Google APIs your application is configured for.


pip install google-apis-oauth-django


Redirecting users to the login screen

from django.shortcuts import HttpResponseRedirect
import google_apis_oauth

def RedirectToOAuthPage(request):
    oauth_url = google_apis_oauth.get_authorization_url(
        # Path of the "client_id.json" file
        # Authorization scopes required
        # The url where the google oauth should redirect
        # after a successful login.
        # Force the consent prompt even if the user was authorized
        # previously. Defaults to False.
    return HttpResponseRedirect(url)

Retreving and storing the credentials after successful login

import google_apis_oauth

def RedirectView(request):
        # Get user credentials
        credentials = google_apis_oauth.get_crendentials_from_callback(

        # Stringify credentials for storing them in the DB
        stringified_token = google_apis_oauth.stringify_credentials(

        # Store the credentials safely in the DB

        # Now that you have stored the user credentials you
        # can redirect user to your main application.
    except google_apis_oauth.exceptions.InvalidLoginException:
        # Handle unauthenticated request to the callback uri.

Loading and using the user credentials

import google_apis_oauth
from googleapiclient.discovery import build

# Use the stringified token to get a credentials object
# that can be used to authenticate requests made by
# google-api-python-client
# refreshed is a boolean that tells if the token was expired and was renewed.
# You may want to update the credentials in the database if it is True.
creds, refreshed = google_apis_oauth.load_credentials(stringified_token)

# Using credentials in google-api-python-client.
service = build('calendar', 'v3', credentials=creds)


You can refer to this blog for an example where this library is used.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google-apis-oauth-django-1.0.0.tar.gz (4.1 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page