2

私はDjangoに少し慣れていないので、pythonスクリプト(認証、gmailサービスの構築、gmailのクエリ)をdjangoアプリに変換するのに助けが必要です。Google django サンプルと、ここで指定したクラスhttps://developers.google.com/api-client-library/python/guide/djangoを使用しようとしていますが、少し混乱しています。どんな助けでも大歓迎です!以下のコードは、インデックスにアクセスしたときの認証フローを示していますが、Google プロファイルをクリックすると、結果のページにエラーが表示され"ValueError at /app/ ます。

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.

ビューのコード:

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.

モデル:

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)
4

1 に答える 1