Google日历API:HTTPERROR 400在Google日历中创建事件

发布于 2025-02-03 18:19:39 字数 7365 浏览 2 评论 0原文

我已经制作了一个程序,该程序将我从工作中转移,并将它们作为事件导入Google日历。 它在去年工作得很好,但是当我今天运行时,我得到了400 httperror。

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json returned "Bad Request". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'Bad Request'}]">

我不知道导致它发生的原因。我已经从其他论坛上读到,这可能是时区的某些内容,但是自从该程序制作以来,我没有更改一行。我已经更新了Chromedriver几次。

from __future__ import nested_scopes, print_function
import datetime
from os import R_OK
import os.path
from random import Random
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

from selenium import webdriver
from selenium.webdriver.common.by import By
import re

# ------------------------------------------------------------------------------------------------

n = 0
ArbejdsTider = []

# Information
UsernameText = "my email"
PasswordText = "my password"

user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"

options = webdriver.ChromeOptions()
options.headless = True
options.add_argument(f'user-agent={user_agent}')
options.add_argument("--window-size=1920,1080")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

# Kom ind på siden
driver.get(
    "https://app.tamigo.com/Shift/Pages/EmployeeShifts.aspx")

# Find username textbox og indsæt username
UsernameInput = driver.find_element(By.ID, 'Username')
UsernameInput.send_keys(UsernameText)

# Find password textbox og indsæt password
PasswordInput = driver.find_element(By.ID, 'Password')
PasswordInput.send_keys(PasswordText)

# Find log-in knap og tryk
LoginBtn = driver.find_element(By.ID, 'login-btn').click()

# Find tabel
RawData = driver.find_element(By.XPATH, '//*[@id="employeeShiftsGrid"]/tbody')

# Gemmer de forskellige vagter i lister [[dag1, tid2], [dag2,tid2], [dag3,tid3]]
for row in RawData.find_elements(By.TAG_NAME, "tr"):
    n += 1
    dag = driver.find_element(
        By.XPATH, "//*[@id='employeeShiftsGrid']/tbody/tr[{}]/td[4]".format(n)).text
    tid = driver.find_element(
        By.XPATH, "//*[@id='employeeShiftsGrid']/tbody/tr[{}]/td[5]".format(n)).text
    ArbejdsTider.append([dag, tid])

# Filtrer dagenavne væk
for x in range(len(ArbejdsTider)):
    ArbejdsTider[x][0] = re.sub('\D', '', ArbejdsTider[x][0])
    ArbejdsTider[x][1] = re.sub('\D', '', ArbejdsTider[x][1])

# Filterer 2-tal væk i tid ved evt. pause i vagten
for x in range(len(ArbejdsTider)):
    if len(ArbejdsTider[x][1]) > 8:
        ArbejdsTider[x][1] = ArbejdsTider[x][1][:-1]

# for x in range(len(ArbejdsTider)):
    # print(ArbejdsTider[x][0])
    # print(ArbejdsTider[x][1])
# driver.get_screenshot_as_file("screenshot.png")

# ------------------------------------------------------------------------------------------------

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar']

creds = None
# The file token.json 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.json'):
    creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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(
            'C:/Users/rasmu/Desktop/TTC/Calendar/credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.json', 'w') as token:
        token.write(creds.to_json())

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

# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=now,
                                      maxResults=100, singleEvents=True,
                                      orderBy='startTime').execute()
events = events_result.get('items', [])

if not events:
    print('No upcoming events found.')
for event in events:
    start = event['start'].get('dateTime', event['start'].get('date'))
    if event['summary'] == "Arbejde":

        # Slet vagt
        service.events().delete(calendarId='primary',
                                eventId=event['id']).execute()
        # print("Delete the event and create a new one")
    # print(start, event['summary'])

for x in range(len(ArbejdsTider)):
    tidspunkt = str(ArbejdsTider[x][1][4]) + str(ArbejdsTider[x][1][5])
    STARTÅR = int(str(ArbejdsTider[x][0][4]) + str(ArbejdsTider[x][0]
                  [5]) + str(ArbejdsTider[x][0][6]) + str(ArbejdsTider[x][0][7]))
    STARTMÅNED = int(str(ArbejdsTider[x][0][2]) + str(ArbejdsTider[x][0][3]))
    STARTDAG = int(str(ArbejdsTider[x][0][0]) + str(ArbejdsTider[x][0][1]))
    STARTTIME = int(str(ArbejdsTider[x][1][0]) + str(ArbejdsTider[x][1][1]))
    STARTMINUT = int(str(ArbejdsTider[x][1][2]) + str(ArbejdsTider[x][1][3]))
    SLUTÅR = int(str(ArbejdsTider[x][0][4]) + str(ArbejdsTider[x][0]
                 [5]) + str(ArbejdsTider[x][0][6]) + str(ArbejdsTider[x][0][7]))
    SLUTMÅNED = int(str(ArbejdsTider[x][0][2]) + str(ArbejdsTider[x][0][3]))
    SLUTDAG = int(str(ArbejdsTider[x][0][0] + str(ArbejdsTider[x][0][1])))
    SLUTTIME = int(str(ArbejdsTider[x][1][4]) + str(ArbejdsTider[x][1][5]))
    SLUTMINUT = int(str(ArbejdsTider[x][1][6]) + str(ArbejdsTider[x][1][7]))
    # Vagt slutter efter midnat
    if int(tidspunkt) >= 0 and int(tidspunkt) < 4:
        event = {
            'summary': 'Arbejde',
            'start': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(STARTÅR, STARTMÅNED, STARTDAG, STARTTIME, STARTMINUT),
                'timeZone': 'GMT+02:00',
            },
            'end': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(SLUTÅR, SLUTMÅNED, SLUTDAG+1, SLUTTIME, SLUTMINUT),
                'timeZone': 'GMT+02:00',
            }
        }
        event = service.events().insert(calendarId='primary', body=event).execute()

        # Vagt slutter før midnat
    else:
        event = {
            'summary': 'Arbejde',
            'start': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(STARTÅR, STARTMÅNED, STARTDAG, STARTTIME, STARTMINUT),
                'timeZone': 'GMT+02:00',
            },
            'end': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(SLUTÅR, SLUTMÅNED, SLUTDAG, SLUTTIME, SLUTMINUT),
                'timeZone': 'GMT+02:00',
            }
        }
        event = service.events().insert(calendarId='primary', body=event).execute()

I've made a program which takes my shifts from work and imports them as events into Google Calendar.
It have worked perfectly the last year, but when i ran it today I got a 400 httperror.

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json returned "Bad Request". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'Bad Request'}]">

I have no clue what have caused it to happen. I've read from other forums that it might be something with the timezones, but i haven't changed a single line in this program since it was made. I have updated the chromedriver a few times though.

from __future__ import nested_scopes, print_function
import datetime
from os import R_OK
import os.path
from random import Random
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

from selenium import webdriver
from selenium.webdriver.common.by import By
import re

# ------------------------------------------------------------------------------------------------

n = 0
ArbejdsTider = []

# Information
UsernameText = "my email"
PasswordText = "my password"

user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36"

options = webdriver.ChromeOptions()
options.headless = True
options.add_argument(f'user-agent={user_agent}')
options.add_argument("--window-size=1920,1080")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(executable_path="chromedriver.exe", options=options)

# Kom ind på siden
driver.get(
    "https://app.tamigo.com/Shift/Pages/EmployeeShifts.aspx")

# Find username textbox og indsæt username
UsernameInput = driver.find_element(By.ID, 'Username')
UsernameInput.send_keys(UsernameText)

# Find password textbox og indsæt password
PasswordInput = driver.find_element(By.ID, 'Password')
PasswordInput.send_keys(PasswordText)

# Find log-in knap og tryk
LoginBtn = driver.find_element(By.ID, 'login-btn').click()

# Find tabel
RawData = driver.find_element(By.XPATH, '//*[@id="employeeShiftsGrid"]/tbody')

# Gemmer de forskellige vagter i lister [[dag1, tid2], [dag2,tid2], [dag3,tid3]]
for row in RawData.find_elements(By.TAG_NAME, "tr"):
    n += 1
    dag = driver.find_element(
        By.XPATH, "//*[@id='employeeShiftsGrid']/tbody/tr[{}]/td[4]".format(n)).text
    tid = driver.find_element(
        By.XPATH, "//*[@id='employeeShiftsGrid']/tbody/tr[{}]/td[5]".format(n)).text
    ArbejdsTider.append([dag, tid])

# Filtrer dagenavne væk
for x in range(len(ArbejdsTider)):
    ArbejdsTider[x][0] = re.sub('\D', '', ArbejdsTider[x][0])
    ArbejdsTider[x][1] = re.sub('\D', '', ArbejdsTider[x][1])

# Filterer 2-tal væk i tid ved evt. pause i vagten
for x in range(len(ArbejdsTider)):
    if len(ArbejdsTider[x][1]) > 8:
        ArbejdsTider[x][1] = ArbejdsTider[x][1][:-1]

# for x in range(len(ArbejdsTider)):
    # print(ArbejdsTider[x][0])
    # print(ArbejdsTider[x][1])
# driver.get_screenshot_as_file("screenshot.png")

# ------------------------------------------------------------------------------------------------

# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar']

creds = None
# The file token.json 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.json'):
    creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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(
            'C:/Users/rasmu/Desktop/TTC/Calendar/credentials.json', SCOPES)
        creds = flow.run_local_server(port=0)
    # Save the credentials for the next run
    with open('token.json', 'w') as token:
        token.write(creds.to_json())

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

# Call the Calendar API
now = datetime.datetime.utcnow().isoformat() + 'Z'  # 'Z' indicates UTC time
events_result = service.events().list(calendarId='primary', timeMin=now,
                                      maxResults=100, singleEvents=True,
                                      orderBy='startTime').execute()
events = events_result.get('items', [])

if not events:
    print('No upcoming events found.')
for event in events:
    start = event['start'].get('dateTime', event['start'].get('date'))
    if event['summary'] == "Arbejde":

        # Slet vagt
        service.events().delete(calendarId='primary',
                                eventId=event['id']).execute()
        # print("Delete the event and create a new one")
    # print(start, event['summary'])

for x in range(len(ArbejdsTider)):
    tidspunkt = str(ArbejdsTider[x][1][4]) + str(ArbejdsTider[x][1][5])
    STARTÅR = int(str(ArbejdsTider[x][0][4]) + str(ArbejdsTider[x][0]
                  [5]) + str(ArbejdsTider[x][0][6]) + str(ArbejdsTider[x][0][7]))
    STARTMÅNED = int(str(ArbejdsTider[x][0][2]) + str(ArbejdsTider[x][0][3]))
    STARTDAG = int(str(ArbejdsTider[x][0][0]) + str(ArbejdsTider[x][0][1]))
    STARTTIME = int(str(ArbejdsTider[x][1][0]) + str(ArbejdsTider[x][1][1]))
    STARTMINUT = int(str(ArbejdsTider[x][1][2]) + str(ArbejdsTider[x][1][3]))
    SLUTÅR = int(str(ArbejdsTider[x][0][4]) + str(ArbejdsTider[x][0]
                 [5]) + str(ArbejdsTider[x][0][6]) + str(ArbejdsTider[x][0][7]))
    SLUTMÅNED = int(str(ArbejdsTider[x][0][2]) + str(ArbejdsTider[x][0][3]))
    SLUTDAG = int(str(ArbejdsTider[x][0][0] + str(ArbejdsTider[x][0][1])))
    SLUTTIME = int(str(ArbejdsTider[x][1][4]) + str(ArbejdsTider[x][1][5]))
    SLUTMINUT = int(str(ArbejdsTider[x][1][6]) + str(ArbejdsTider[x][1][7]))
    # Vagt slutter efter midnat
    if int(tidspunkt) >= 0 and int(tidspunkt) < 4:
        event = {
            'summary': 'Arbejde',
            'start': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(STARTÅR, STARTMÅNED, STARTDAG, STARTTIME, STARTMINUT),
                'timeZone': 'GMT+02:00',
            },
            'end': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(SLUTÅR, SLUTMÅNED, SLUTDAG+1, SLUTTIME, SLUTMINUT),
                'timeZone': 'GMT+02:00',
            }
        }
        event = service.events().insert(calendarId='primary', body=event).execute()

        # Vagt slutter før midnat
    else:
        event = {
            'summary': 'Arbejde',
            'start': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(STARTÅR, STARTMÅNED, STARTDAG, STARTTIME, STARTMINUT),
                'timeZone': 'GMT+02:00',
            },
            'end': {
                'dateTime': '{}-{}-{}T{}:{}:00'.format(SLUTÅR, SLUTMÅNED, SLUTDAG, SLUTTIME, SLUTMINUT),
                'timeZone': 'GMT+02:00',
            }
        }
        event = service.events().insert(calendarId='primary', body=event).execute()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸵鸟症 2025-02-10 18:19:39

错误代码 400 表示尚未提供所需的字段或参数,提供的值无效,或者提供的字段的组合无效。

您可以通过在请求主体的 datetime 值中提供其中一个来复制错误:

  • 空字符串
  • day&gt; 31
  • 月&gt; 12
  • 小时&gt; 23
  • 分钟&gt; 59

示例:

无效日期:

”在此处输入图像说明“

输出:

An error occurred: <HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json returned "Bad Request". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'Bad Request'}]">

要解决问题,请确保验证传递给 dateTime 的值代码部分。

参考:

The error code 400 indicates that a required field or parameter has not been provided, the value supplied is invalid, or the combination of provided fields is invalid.

You can replicate the error by providing either one of these in the dateTime value of your request body:

  • empty string
  • day > 31
  • month > 12
  • hours > 23
  • minutes > 59

Example:

Invalid date:

enter image description here

Output:

An error occurred: <HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars/primary/events?alt=json returned "Bad Request". Details: "[{'domain': 'global', 'reason': 'badRequest', 'message': 'Bad Request'}]">

To resolve the issue, make sure to validate the values being passed to the dateTime section of your code.

Reference:

空心↖ 2025-02-10 18:19:39

在我的情况下,这是日期格式发行日期格式必须是ISO 8601格式,例如

2025-01-15T10:00:00+00:00:00:00:00:00:00:00,//在ISO 8601格式中启动时间

我的最终有效负载类似于

{"summary":"Appointment","location":"Conference Room, Office HQ","description":"Discussion on general inquiry.","start":{"dateTime":"2025-01-15T10:00:00+00:00","timeZone":"Asia\/Karachi"},"end":{"dateTime":"2025-01-15T11:00:00+00:00","timeZone":"Asia\/Karachi"},"attendees":[{"email":"[email protected]","displayName":"ahsan","comment":"Phone Number: +9277665667"}],"reminders":{"useDefault":true}}

In my case it was date format issue the date formate must be ISO 8601 format like the example below

2025-01-15T10:00:00+00:00, // Start time in ISO 8601 format

My final payload is something like

{"summary":"Appointment","location":"Conference Room, Office HQ","description":"Discussion on general inquiry.","start":{"dateTime":"2025-01-15T10:00:00+00:00","timeZone":"Asia\/Karachi"},"end":{"dateTime":"2025-01-15T11:00:00+00:00","timeZone":"Asia\/Karachi"},"attendees":[{"email":"[email protected]","displayName":"ahsan","comment":"Phone Number: +9277665667"}],"reminders":{"useDefault":true}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文