Google Calendar API-创建事件400,解析错误

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

使用python 3.6,requests == 2.22.0

尝试创建事件:documentation link

url = 'https://www.googleapis.com/calendar/v3/calendars/{}/events'.format(calendar_id)
data = {
      "summary": "CALENDAR TESTING",
      "location": "Some fake location",
      "description": "This is a test",
      "start": {
        "dateTime": "2019-10-09T09:00:00-07:00",
        "timeZone": "America/Los_Angeles",
      },
      "end": {
        "dateTime": "2019-10-09T10:00:00-07:00",
        "timeZone": "America/Los_Angeles",
      },
}
headers = {
        'Authorization': 'Bearer {}'.format(self.access_token.get('token')),
        'Accept': 'application/json',
        'Content-Type': 'application/json',
    }
response = requests.post(url, data=data, headers=headers)

print("Response: ", response)
j = response.json()
print("json: ", j)

输出:

Response:  <Response [400]>
Raw Response:  {'error': {'errors': [{'domain': 'global', 'reason': 'parseError', 'message': 'Parse Error'}], 'code': 400, 'message': 'Parse Error'}}

请求正文:

    summary=CALENDAR+TESTING&location=Some+fake+location&description=This+is+a+test&start=dateTime&start=timeZone&end=dateTime&end=timeZone

请求标头:

    {'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/json', 'Connection': 'keep-alive', 'Authorization': 'Bearer ******', 'Content-Type': 'application/json', 'Content-Length': '135'}

没有解决该问题的堆栈文章:stack post-重新设置内容类型,我是stack post-重新撇号与双引号,我切换到双引号仍然错误

我确实注意到请求主体中缺少时区信息,但是我不确定为什么,如果这是问题所在。

我现在正在调查this post

python-3.x google-api python-requests google-calendar-api
1个回答
0
投票

解决问题的简单方法:

import json
response = requests.post(url, data=json.dumps(data), headers=headers)
© www.soinside.com 2019 - 2024. All rights reserved.