Repository navigation
DevSite Import: Using Django #689
Description
Notice: This DevSite page will soon be deleted per b/133171590. Please add this sample to GitHub if it is relevant still:
https://developers.google.com/api-client-library/python/guide/django
Using Django
The Google APIs Client Library for Python has special support for the Django web framework. In particular, there are classes that simplify the OAuth 2.0 protocol steps. This document describes the Django-specific classes available for working with Flow, Credentials, and Storage objects.
Flows
Use the
oauth2client.contrib.django_orm.FlowField
class as a Django model field so that Flow objects can easily be stored.
When your application is simultaneously going through OAuth 2.0 steps for many users,
it's normally best to store per-user Flow objects before the first redirection.
This way, your redirection handlers can retrieve the Flow object already created for the user.
In the following code,
a model is defined that allows Flow objects to be stored and keyed by User:
from django.contrib.auth.models import User from django.db import models from oauth2client.contrib.django_orm import FlowField ... class FlowModel(models.Model): id = models.ForeignKey(User, primary_key=True) flow = FlowField()
Credentials
Use the
oauth2client.contrib.django_orm.CredentialsField
class as a Django model field so that Credentials objects can easily be stored.
Similar to Flow objects, it's normally best to store per-user Credentials objects.
In the following code,
a model is defined that allows Credentials objects to be stored and keyed by User:
from django.contrib.auth.models import User from django.db import models from oauth2client.contrib.django_orm import CredentialsField ... class CredentialsModel(models.Model): id = models.ForeignKey(User, primary_key=True) credential = CredentialsField()
Storage
Use the
oauth2client.contrib.django_orm.Storage
class to store and retrieve Credentials objects
using a model defined with a CredentialsField object.
You pass the model, field name for the model key, value for the model key,
and field name to the CredentialsField constructor.
The following shows how to create, read, and write Credentials objects
using the example CredentialsModel class above:
from django.contrib.auth.models import User from oauth2client.contrib.django_orm import Storage from your_project.your_app.models import CredentialsModel ... user = # A User object usually obtained from request. storage = Storage(CredentialsModel, 'id', user, 'credential') credential = storage.get() ... storage.put(credential)
Activity
busunkim96 commentedon Jun 6, 2019
@grant Thanks for the heads up! We deleted the Django sample from this repo a while back, so I don't think we have any concerns.
docs: Adds docs folder. Fixes googleapis#669 Fixes googleapis#670 Fixes
grant commentedon Jun 26, 2019
The docs are now added to the
docs/folder.Fixed with #706.
matej2 commentedon Oct 7, 2020
Why was Django example removed? Did this libary lost support for Django?
akashsenta13 commentedon Aug 25, 2021
Do we have any doc for django integration ?