gpt4 book ai didi

python - Django 1.7 google oauth2 token 验证失败

转载 作者:太空狗 更新时间:2023-10-29 20:37:08 25 4
gpt4 key购买 nike

我正在尝试完成对用于在 Django 应用程序中访问用户日历的 Google token 进行身份验证的过程。尽管我遵循了在网络上找到的几个指示,但我仍然对我的回调函数(错误请求)有 400 错误代码响应。

views.py

# -*- coding: utf-8 -*-
import os

import argparse
import httplib2
import logging

from apiclient.discovery import build
from oauth2client import tools
from oauth2client.django_orm import Storage
from oauth2client import xsrfutil
from oauth2client.client import flow_from_clientsecrets

from django.http import HttpResponse
from django.http import HttpResponseBadRequest
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.core.urlresolvers import reverse
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.conf import settings

from apps.tecnico.models import Credentials, Flow

CLIENT_SECRETS = os.path.join(
os.path.dirname(__file__), '../../client_secrets.json')

@login_required
def index(request):
storage = Storage(Credentials, 'id', request.user, 'credential')
FLOW = flow_from_clientsecrets(
CLIENT_SECRETS,
scope='https://www.googleapis.com/auth/calendar.readonly',
redirect_uri='http://MY_URL:8000/oauth2/oauth2callback'
)
credential = storage.get()
if credential is None or credential.invalid is True:
FLOW.params['state'] = xsrfutil.generate_token(
settings.SECRET_KEY, request.user)
authorize_url = FLOW.step1_get_authorize_url()
f = Flow(id=request.user, flow=FLOW)
f.save()
return HttpResponseRedirect(authorize_url)
else:
http = httplib2.Http()
http = credential.authorize(http)
service = build(serviceName='calendar', version='v3', http=http,
developerKey='MY_DEV_KEY_FROM_GOOGLE_CONSOLE')

events = service.events().list(calendarId='primary').execute()
return render_to_response('calendario/welcome.html', {
'events': events['items'],
})


@login_required
def auth_return(request):
if not xsrfutil.validate_token(
settings.SECRET_KEY, request.REQUEST['state'], request.user):
return HttpResponseBadRequest()

storage = Storage(Credentials, 'id', request.user, 'credential')
FLOW = Flow.objects.get(id=request.user).flow
credential = FLOW.step2_exchange(request.REQUEST)
storage.put(credential)
return HttpResponseRedirect("http://MY_URL:8000/caly")

模型.py

from oauth2client.django_orm import FlowField, CredentialsField

[...]

class Credentials(models.Model):
id = models.ForeignKey(User, primary_key=True)
credential = CredentialsField()


class Flow(models.Model):
id = models.ForeignKey(User, primary_key=True)
flow = FlowField()

我直接从 Google Dev Console 下载了 client_secrets.json 文件。Dev Console中指定的Client ID类型是“web application”,我认为是正确的。我注意到,如果我删除 token 验证代码块:

if not xsrfutil.validate_token(
settings.SECRET_KEY, request.REQUEST['state'], request.user):
return HttpResponseBadRequest()

一切正常,流程和凭据正确存储在数据库中,我可以阅读日历。我可能有什么问题?

编辑:我还检查了传出(到 Google)和传入(到回调)数据:

外出:

request.user:
admin
settings.SECRET_KEY:
I_AM_NOT_WRITING_IT_HERE
FLOW.params['state']:
SOME_OTHER_RANDOM_STUFF

传入:

request.user:
admin
settings.SECRET_KEY:
I_AM_NOT_WRITING_IT_HERE
FLOW.params['state']:
SOME_OTHER_RANDOM_STUFF

数据是相同的,至少是打印到控制台。此外,通过控制台的生成/验证操作正常工作(xsrfutil.validate_token 返回 True,包括测试和真实数据,包括用户模型实例)。我更疑惑了。

最佳答案

我已经为完全相同的问题苦苦挣扎了几个小时,我找到了@Ryan Spaulding 和@Hans Z 回答的解决方案。有用!

This is due to the fact Django 1.7 returns a unicode object for the state variable above using request.REQUEST. I was previously using Django 1.6 which used to return a string.

可以在此处找到更多详细信息。 https://github.com/google/google-api-python-client/issues/58我写这篇文章以供将来引用。

if not xsrfutil.validate_token(
settings.SECRET_KEY,
str(request.REQUEST['state']),
request.user):
return HttpResponseBadRequest()

关于python - Django 1.7 google oauth2 token 验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27441567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com