TypeError:“ AnonymousUser”对象不可迭代

时间:2018-12-26 07:45:39

标签: python django

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”对象不可迭代              在阅读凭据

的同时

我遇到了错误。

  

文件“ C:\ Users \ admin \ Desktop \ GmailTest \ google-             login \ gfglogin \ gfgauth \ views.py“,第10行,用gmail_authenticate               凭证= storage.get()

     

TypeError:“ AnonymousUser”对象不可迭代             [26 / Dec / 2018 12:51:07]“ GET / auth / HTTP / 1.1” 500 111109

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

1 个答案:

答案 0 :(得分:1)

好吧,正如我从DjangoORMStorage的类文档中看到的那样:

  

oauth2client。从Django ORM检索到的凭据,已关联                使用modelkey_value-> key_name对来查询                该模型,然后property_name标识                CredentialsProperty字段,所有字段均在                此Storage对象的构造函数。

因此,您需要传递id初始化的DjangoORMStorage的值,并且它不应是对象。我认为您的实现应如下所示:

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