首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不断收到错误“超过每日未认证使用的限制。继续使用需要注册。”

不断收到错误“超过每日未认证使用的限制。继续使用需要注册。”
EN

Stack Overflow用户
提问于 2020-02-14 02:37:11
回答 1查看 122关注 0票数 0

我一直收到这个错误,而且我一天的查询量肯定不是很高(可能在我收到这个错误之前大约有5次)

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
@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': 'test@test.edu'},
        ],
        '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。

EN

回答 1

Stack Overflow用户

发布于 2020-05-15 06:23:47

我认为(至少有时)这个错误是误导性的--我认为这里就是这种情况。您似乎缺少start_timeend_time变量末尾的UTC偏移量。

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

尝试替换:

代码语言:javascript
复制
start_time = appt.scheduledStart.strftime('%Y-%m-%dT%H:%M:%S')
end_time = appt.scheduledEnd.strftime('%Y-%m-%dT%H:%M:%S')

通过以下方式:

代码语言:javascript
复制
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包-然后使用以下内容调用它:

代码语言:javascript
复制
    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获取标准时区的列表

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60214474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档