繁体   English   中英

类型错误:“匿名用户”对象不可迭代

[英]TypeError: 'AnonymousUser' object is not iterable

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 credential = storage.get()

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

我期待对 gmail 用户进行身份验证。

好吧,正如我从DjangoORMStorage类文档中DjangoORMStorage

从 Django ORM 中检索到的 oauth2client.Credentials,与model相关联,用于查询model key_value -> key_name对,以及标识CredentialsProperty字段的property_name ,所有这些都在此 Storage 对象的构造函数中定义。

所以你需要传递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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM