A library to help integrate Google OAuth 2.0 to your Django application.
Project description
Google APIs OAuth Django
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.
Installation
pip install google-apis-oauth-django
Usage
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
JSON_FILEPATH,
# Authorization scopes required
SCOPES,
# The url where the google oauth should redirect
# after a successful login.
REDIRECT_URI,
# Force the consent prompt even if the user was authorized
# previously. Defaults to False.
True)
return HttpResponseRedirect(url)
Retreving and storing the credentials after successful login
import google_apis_oauth
def RedirectView(request):
try:
# Get user credentials
credentials = google_apis_oauth.get_crendentials_from_callback(
request,
JSON_FILEPATH,
SCOPES,
REDIRECT_URI
)
# Stringify credentials for storing them in the DB
stringified_token = google_apis_oauth.stringify_credentials(
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
# NOTE: This function automatically refreshes the access_token
# if required.
creds = google_apis_oauth.load_credentials(stringified_token)
# Using credentials in google-api-python-client.
service = build('calendar', 'v3', credentials=creds)
...
Example
You can refer to this blog for an example where this library is used.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for google-apis-oauth-django-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c96c301c3446d3835cc498f84c9b28932fd13f6ae78eb31390d08cdff6e5f43 |
|
MD5 | 686d00797e2589b9fd6a0f7abb24fcdb |
|
BLAKE2b-256 | 48c1fb659f77bca46c3973d84978be3fe3f2bf4b5b5435a36326e08c78002df7 |
Close
Hashes for google_apis_oauth_django-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4acb58c847373abb2c8ace78e488cf9892400297515770dd16317c6883e0af3c |
|
MD5 | 1eea45d1ce6340e7cd22a18117fdb0d8 |
|
BLAKE2b-256 | 46ffbcba1519e6ee972e14c3c41b7ac95e974bd0cff2b3912cc9cb065b01f38e |