Я хочу, чтобы посетитель этого веб-сайта проходил проверку подлинности через Gmail API.
Получение следующей ошибки в строке 24 в views.py (в строке FLOW.params ['state']):
NameError at /gmailAuthenticate/
name 'xsrfutil' is not defined
Файл Views.py:
from django.shortcuts import render
from .models import CredentialsModel
import httplib2
import requests
from oauth2client.client import flow_from_clientsecrets
from gfglogin import settings
from django.http import HttpResponseRedirect
from oauth2client.contrib.django_util.storage import DjangoORMStorage
from oauth2client.contrib.django_util.models import CredentialsField
# Create your views here.
FLOW = flow_from_clientsecrets(
settings.GOOGLE_OAUTH2_CLIENT_SECRETS_JSON,
scope='https://www.googleapis.com/auth/gmail.readonly',
redirect_uri='http://127.0.0.1:8000/oauth2callback',
prompt='consent')
def gmail_authenticate(request):
storage = DjangoORMStorage(CredentialsModel, 'id', request.user, 'credential')
credential = storage.get()
if credential is None or credential.invalid:
FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY,
request.user)
authorize_url = FLOW.step1_get_authorize_url()
return HttpResponseRedirect(authorize_url)
else:
http = httplib2.Http()
http = credential.authorize(http)
service = build('gmail', 'v1', http = http)
print('access_token = ', credential.access_token)
status = True
return render(request, 'index.html', {'status': status})
Urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^gmailAuthenticate/', views.gmail_authenticate, name ='gmail_authenticate'),
url(r'^oauth2callback/', views.auth_return),
url(r'^$', views.home, name ='home'),
]
Невозможно понять, в чем причина.