I'm a little new to Django and need some help translating my python script (authenticating, building the gmail service, querying gmail) into a django app. I'm trying to use the google django samples and the classes they've specified here https://developers.google.com/api-client-library/python/guide/django but getting a little confused. Any help would be greatly appreciated! The code below brings me through the auth flow when I visit my index, but then after clicking on my google profile, the resulting page is an error: "ValueError at /app/
Cannot assign None: "CredentialsModel.id" does not allow null values."
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/sachinshukla/Desktop/gmail/customer_support/views.py" in index
70. credentials = run(flow, storage, http=http)
File "/Library/Python/2.7/site-packages/oauth2client/util.py" in positional_wrapper
132. return wrapped(*args, **kwargs)
File "/Library/Python/2.7/site-packages/oauth2client/old_run.py" in run
156. storage.put(credential)
File "/Library/Python/2.7/site-packages/oauth2client/client.py" in put
325. self.locked_put(credentials)
File "/Library/Python/2.7/site-packages/oauth2client/django_orm.py" in locked_put
126. entity = self.model_class(**args)
File "/Library/Python/2.7/site-packages/django/db/models/base.py" in __init__
405. setattr(self, field.name, rel_obj)
File "/Library/Python/2.7/site-packages/django/db/models/fields/related.py" in __set__
335. (instance._meta.object_name, self.field.name))
Exception Type: ValueError at /customer_support/
Exception Value: Cannot assign None: "CredentialsModel.id" does not allow null values.
Code for view:
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), '..', 'client_secret.json')
REDIRECT_URI = 'http://localhost:8080/'
SCOPES = [
'https://www.googleapis.com/auth/gmail.modify',
]
def index(request):
storage = Storage(CredentialsModel, 'id', request.user.id, 'credential')
credentials = storage.get()
flow = flow_from_clientsecrets(CLIENT_SECRETS, scope=SCOPES)
http = httplib2.Http()
if credentials is None or credentials.invalid:
credentials = run(flow, storage, http=http)
# Authorize the httplib2.Http object with our credentials
http = credentials.authorize(http)
# Build the Gmail service from discovery
gmail_service = build('gmail', 'v1', http=http)
# then do something, query threads, messages, etc,
and return them to a template - not sure where exactly to put the methods for querying threads, messages, etc.
Model:
from django.db import models
import pickle
import base64
from django.contrib import admin
from django.contrib.auth.models import User
from django.db import models
from oauth2client.django_orm import FlowField
from oauth2client.django_orm import CredentialsField
class CredentialsModel(models.Model):
id = models.ForeignKey(User, primary_key=True)
credential = CredentialsField()
class CredentialsAdmin(admin.ModelAdmin):
pass
admin.site.register(CredentialsModel, CredentialsAdmin)