在尝试将googlecalendarv3与Python/Django一起使用时,我遇到了一个错误:“str”对象没有“calendars”属性

2024-04-26 22:46:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将我的django应用程序与googlecalendarapiv3连接起来。在

为此,我使用Google API Python client

在文档中,我看到了关于如何创建service object的参考。在

我在这里看到了一个如何使用它的例子Django example from Google。 这是我观点的一部分。在

import logging
import httplib2
from oauth2client.django_orm import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.util import logger
from apiclient.discovery import build
from core.models import FlowModel, CredentialsModel
from django.http import HttpResponse, HttpResponseRedirect 
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext

FLOW = OAuth2WebServerFlow(
        client_id= _cliente_id,
        client_secret= _cliente_secret,
        scope= _scope,
        user_agent=_user_agent)

current_site = Site.objects.get_current()
URI = "http://"+str(current_site)+"/auth_return"

def authentice(request):
    user = request.user
    storage = Storage(CredentialsModel, 'id', user, 'credential')
    credential = storage.get()
    if credential is None or credential.invalid == True:

        authorize_url = FLOW.step1_get_authorize_url(URI)
        f = FlowModel(id=user, flow=flow)
        f.save()
        return authorize_url
    else:
        http = httplib2.Http()
        http = credential.authorize(http)

        # The Service Object

        service = build(serviceName='calendar', version='v3', http=http)

        return service


def auth_return(request):
    f = FlowModel.objects.get(id=request.user)

    credential = f.FLOW.step2_exchange(request.REQUEST) #Zica aqui...

    storage = Storage(CredentialsModel, 'id', request.user, 'credential')
    storage.put(credential)
    f.delete()
    return HttpResponseRedirect("/")


def new_calendar(request):
    service = authentice(request)

    if request.POST:

        service = authentice(request)    

        calendar = {
            'summary': request.POST.get("summary"),
            'timeZone': 'America/Santo_Domingo'
        }

        created_calendar = service.calendars().insert(body=calendar).execute()


        return HttpResponseRedirect("/") 
    else:
        return render_to_response('calendar_form.html',
                              context_instance=RequestContext(request))

这将返回下一个错误:

'str' object has no attribute 'calendars'

它实际上是返回一个字符串(一个URL)而不是一个服务对象,我不知道为什么。在

如果你们有什么想法,请帮我。在

PS:对不起我的英语!:P


Tags: djangofromimportclientidhttpgetreturn