gpt4 book ai didi

python - 类型错误 : 'AnonymousUser' object is not iterable

转载 作者:行者123 更新时间:2023-12-01 08:30:23 26 4
gpt4 key购买 nike

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

我得到了

TypeError: 'AnonymousUser' object is not iterable while reading the credentials

我遇到了以下错误。

File "C:\Users\admin\Desktop\GmailTest\google- login\gfglogin\gfgauth\views.py", line 10, in gmail_authenticate credential = storage.get()

TypeError: 'AnonymousUser' object is not iterable [26/Dec/2018 12:51:07] "GET /auth/ HTTP/1.1" 500 111109

我希望对 Gmail 用户进行身份验证。

最佳答案

嗯,正如我从 DjangoORMStorage 的类文档中看到的:

oauth2client.Credentials retrieved from the Django ORM, associated with the model, key_value->key_name pair used to query for the model, and property_name identifying the CredentialsProperty field, all of which are defined in the constructor for this Storage object.

因此,您需要传递DjangoORMStorage Initiation的id值,并且它不应该是一个对象。我认为你的实现应该是这样的:

def gmail_authenticate(request):
if not request.user.is_authenticated:
# Raise or Return Not authenticated response

storage = DjangoORMStorage(CredentialsModel, 'id', request.user.id, 'credential')
credential = storage.get()

# Rest of the Code

关于python - 类型错误 : 'AnonymousUser' object is not iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53929020/

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