【问题标题】:403 Calendar Usage Limits Exceeded on Google Calendar API403 Google Calendar API 超出日历使用限制
【发布时间】:2020-06-22 21:30:10
【问题描述】:

我使用 Python 构建了一个应用,并将其连接到谷歌日历 API。

我不明白为什么会收到此错误“Google Calendar API: 403 Calendar Usage Limits Exceeded Using Service Account”

我几乎没有添加事件,一周可能 300 个。

我以前有一个旧帐户,几天之内就添加了数千个。现在,有了这个新的免费帐户,它给了我这个错误!

我能做什么?可以修吗?!

启动日历服务:

def initiate_calendar_service():
    """Shows basic usage of the Google Calendar API.
        Prints the start and name of the next 10 events on the user's calendar.
        """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)
    return service

添加事件:


        for i in range(1):

            startStrip = datetime.datetime.strptime(event_start, "%Y-%m-%dT%H:%M:%S")
            endStrip = datetime.datetime.strptime(event_end, "%Y-%m-%dT%H:%M:%S")
            dayOfWeek = startStrip + datetime.timedelta(days=i)
            # les bons formats
            currentStart = str(startStrip + datetime.timedelta(days=i)).replace(" ", "T")
            currentEnd = str(endStrip + datetime.timedelta(days=i)).replace(" ", "T")
            calendarEnd = str(endStrip + datetime.timedelta(days=i + 1)).replace(" ", "T")


            events_result = service.events().list(calendarId='primary', timeMin=currentStart + "-00:00",
                                                  maxResults=30, timeMax=calendarEnd + "-00:00",
                                                  singleEvents=True, orderBy='startTime').execute()
            events = events_result.get('items', [])

            currentEmployees = []
            for event in events:
                currentEmployees.append(event['summary'])

            if employee in currentEmployees:
                event_done = False
                event['summary'] = employee
                
                for event in events:
                   if str2datetime(currentStart) <= str2datetime(event['end']['dateTime'].split('+')[0]) and str2datetime(currentEnd) >= str2datetime(event['start']['dateTime'].split('+')[0]):
                    event_done = False
                    print(employee + ' est occupé')
                    break
                   else:
                      event_done = True
                      break

            if employee not in currentEmployees:
                event_done = True

            if event_done:
                option = show_message_box(QMessageBox.Critical,
                                      "Confirmation",
                                      "Voulez-vous bloquer cette plage horraire?"\
                                      "L'employé : \"" + employee + "\" sera marqué comme indisponible en raison de : " + reason, \
                                      "Nom de l'employé: " + employee + "\n" \
                                      "Raison: " + reason + "\n" \
                                      "À partir du : " + currentStart + "\n" \
                                      "À ce jour " + currentEnd + "\n"
                                      )

                if option == QMessageBox.Yes:
                    event_done = True
                else:
                    print("Événement ignoré!")
                    event_done = False
                    break

                if event_done:
                    event = {
                        'summary': employee,
                        'location': location,
                        'description': reason,
                        'start': {
                            'dateTime': currentStart,
                            'timeZone': 'America/New_York',
                        },
                        'end': {
                            'dateTime': currentEnd,
                            'timeZone': 'America/New_York',
                        },
                        'attendees': [
                            {'email': event_email},
                        ],
                        'reminders': {
                            'useDefault': True,
                        },
                    }
                    register_event(service, event)

            else:
                second_message_box(QMessageBox.Critical,
                                      "ATTENTION!",
                                      "L'inspecteur " + employee + " est déjà occupé à ce moment-là.""\n" \
                                      "Veuillez essayer une autre plage horraire.", QMessageBox.Ok)

其他信息:

我有一个帐户,在 1 个月内,我完成了 3041 个 calendar.events.list 请求。和 181 个 calendar.events.insert。

我没问题。

这一次,使用新帐户,我在 2 天内完成了 730 个 calendar.events.list 请求和 175 个 calendar.events.insert。 2天内175个事件插入是不是很多??

这是给我错误的帐户:

【问题讨论】:

    标签: python google-api google-calendar-api google-api-python-client


    【解决方案1】:

    你可以从documentation看到Calendar usage limits exceeded

    用户达到了 Google 日历限制之一,以保护 Google 用户和基础设施免受滥用行为的影响。

    列出了限制here,重要的是

    对于 G Suite 免费版和 G Suite 试用帐户,限制低于上述限制。

    在免费试用期结束时,如果您的域累计计费至少为 30 美元(或等值),日历使用限制会自动增加。

    所以基本上,如果您有一个新帐户并且仍处于试用期,那么您的限制低于试用期后的限制(Google 未指定确切的限制),这就是您在使用新帐户时遇到此问题的原因帐户。

    除了“为测试发送垃圾邮件”通常不是一个很好的做法并且会引起对“滥用行为”的怀疑之外,对外部访客的限制更严格,因此出于测试目的,您应该邀请您的用户自己的域而不是外部的。

    为避免命中限制,还建议使用exponential backoff 来降低请求的速度。

    【讨论】:

    • 我不明白。我没有服务帐户。我创建了一个 gmail 帐户,实际上花了我 30 秒。然后我从 developers.google.com/calendar/quickstart/js 下载了凭证文件,将其添加到我的 python 目录项目中,将快速启动代码复制到我的应用程序的开头,然后运行该应用程序。这就是我连接 API 所要做的一切。没有 Gsuite 试用版,没有 Gsuite 帐户,什么都没有。关于“测试垃圾邮件”,我用我的备用帐户发送垃圾邮件,几个月来我都没有问题。然后,使用这个新帐户,1 周后,我收到了 403 错误。
    • 您创建了免费的 gmail 帐户还是 GSuite 帐户?
    • gmail 帐号!!
    • 对于客户 (gmail) 帐户,配额较低,尤其是新帐户。这是按预期工作的。但是没有关于确切配额的官方信息,我所能做的就是建议您避免突破限制,并以一种可能看起来“可疑”的方式行事。
    • 是的,这是可以理解的。但是,如果您阅读此内容:support.google.com/a/answer/2905486?hl=en,您会看到“如果您在短时间内在日历中创建超过 100,000 个事件,您可能会在几个小时内失去日历编辑能力。” .... 即使我的帐户配额较低,我什至没有创建 1000 个……此外,Google 日历 API 的礼貌限制为每天 1,000,000 个查询。这很奇怪。
    猜你喜欢
    • 2020-10-11
    • 2019-03-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    相关资源
    最近更新 更多