Skip to content

DevSite Import: Using Django #689

Closed
Closed

Description

@grant

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

  1. busunkim96 commented on Jun 6, 2019

    @busunkim96
    Contributor

    @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.

  2. added
    type: docsImprovement to the documentation for an API.
    and removed
    triage meI really want to be triaged.
    on Jun 10, 2019
  3. added a commit that references this issue on Jun 26, 2019
    a73a4be
  4. grant commented on Jun 26, 2019

    @grant
    ContributorAuthor

    The docs are now added to the docs/ folder.
    Fixed with #706.

  5. matej2 commented on Oct 7, 2020

    @matej2

    Why was Django example removed? Did this libary lost support for Django?

  6. akashsenta13 commented on Aug 25, 2021

    @akashsenta13

    Do we have any doc for django integration ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions