保持错误”,超过了未经身份验证的使用的每日限制。继续使用需要注册。”

问题描述 投票:0回答:1

我一直在收到此错误,并且我绝对不会每天进行大量查询(可能在收到此错误之前大约进行5次查询)

这是我的两个定义。一个获取日历服务,另一个创建事件。

def get_calendar_service(self):
    scopes = ['https://www.googleapis.com/auth/calendar.events']

    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

这是我尝试创建新事件的地方。我首先尝试删除该事件(如果已存在),因为每次有人要将其添加到他们的日历中时都想重新创建它。

@route("/add-google-calendar-event", methods=['POST'])
def add_event_to_google_calendar(self):
    appt_id = str(json.loads(request.data).get('appt_id'))
    appt = self.gcc.get_appointment_by_id(appt_id)
    start_time = appt.scheduledStart.strftime('%Y-%m-%dT%H:%M:%S')
    end_time = appt.scheduledEnd.strftime('%Y-%m-%dT%H:%M:%S')

    service = self.gcc.get_calendar_service()

    try:
        event = service.events().delete(calendarId='primary', eventId=appt_id).execute()
    except:
        pass

    timezone = 'America/Chicago'

    event = {
        'id': appt_id,
        'eventId': appt_id,
        'summary': 'test test test',
        'location': 'test test',
        'start': {
            'dateTime': start_time,
            'timeZone': timezone,
        },
        'end': {
            'dateTime': end_time,
            'timeZone': timezone,
        },
        'attendees': [
            {'email': '[email protected]'},
        ],
        'reminders': {
            'useDefault': False,
            'overrides': [
                {'method': 'email', 'minutes': 24 * 60},
                {'method': 'popup', 'minutes': 10},
            ],
        },
        'Content-Type': 'application/json;charset=UTF-8',
    }

    try:
        event = service.events().insert(calendarId='primary', body=event).execute()
    except Exception as e:
        event = service.events().update(calendarId='primary', body=event, eventId=appt_id).execute()

    return ''

有人知道如何解决此错误吗?如果有帮助,我正在使用OAuth 2.0客户端ID。

python google-api google-calendar-api google-oauth
1个回答
0
投票

[我认为(至少有时)此错误会引起误解-我认为在这种情况下。您似乎在start_timeend_time变量的末尾缺少UTC偏移量。

基本上,start_timeend_time变量的输出如下:2020-05-13T17:06:42。并且您需要它们看起来像这样:2020-05-13T17:06:42-07:00(请注意最后的-07:00)

尝试替换:

start_time = appt.scheduledStart.strftime('%Y-%m-%dT%H:%M:%S')
end_time = appt.scheduledEnd.strftime('%Y-%m-%dT%H:%M:%S')

使用:

start_time = appt.scheduledStart.strftime('%Y-%m-%dT%H:%M:%S')
start_time = f'{start_time}-06:00'  # This is UTC-6 (America/Denver aka 'mountain-time')
end_time = appt.scheduledEnd.strftime('%Y-%m-%dT%H:%M:%S')
end_time = f'{end_time}-06:00'  # This is UTC-6 (America/Denver 'mountain-time')

如果需要帮助来生成偏移量(而不是仅对其进行硬编码),建议您查看pytz包-然后使用类似的代码来调用它:

    time_zone = 'America/New_York'
    utc_now = datetime.datetime.now(pytz.timezone(f'{time_zone}'))
    utc_offset = f'{str(utc_now)[-6:]}'  # Grab just the offset from the timestamp 
    time_start = time_start[:-6] + utc_offset
    time_end = time_end[:-6] + utc_offset

您可以从here获取标准时区的列表

© www.soinside.com 2019 - 2024. All rights reserved.