"Missing End Time" 带有 Google 日历和球拍 Google 包
"Missing End Time" with Google Calendar and Racket Google Package
我正在使用 Google API library for Racket 尝试更新 Google 日历。 (API 不完整,所以我会继续扩展它。)
我似乎无法使用 events.insert put 调用向日历添加事件。执行 put 的代码如下所示:
(define (insert-events calendar-id
event
#:token [t #f]
#:max-attendees [max-attendees #f]
#:send-notifications [send-notifications #f]
#:supports-attachments [supports-attachments #f])
(json-api-post (string->url
(format
"https://www.googleapis.com/calendar/v3/calendars/~a/events"
(form-urlencoded-encode calendar-id)))
event
#:token t))
(define (json-api-post u b
#:token [t #f]
#:headers [headers '()])
(define b* (jsexpr->bytes b))
(let retry ([t t]
[retry-counter 0])
(parse-json-response
(POST-string u b* (if t (cons (token->authorization-header t) headers) headers))
retry
t
retry-counter)))
(define (POST-string u b headers)
(port->string (post-pure-port u b headers)))
但是,无论我如何使用调用,我总是返回错误 400 和消息:"Missing End Time"。我检查了 this question 以确保我正确发送了我的请求。我似乎是。作为参考,我发送的 JSON 对象是:
{
"end": {
"dateTime": "2016-05-30T14:00:00-04:00"
},
"start": {
"dateTime": "2016-05-30T13:00:00-04:00"
}
}
此外,为了确保我正确访问了正确的密钥和日历 ID,我为我的本地计算机设置了一个回显服务器,并将 url 从 google.com
更改为 localhost
,我的回复似乎很正常:
POST /<Calendar-Id-Redacted> HTTP/1.1
Host: localhost
User-Agent: Racket/6.5.0.5 (net/http-client)
Content-Length: 116
Authorization: Bearer <Key-Redacted>
{
"end": {
"dateTime": "2016-05-30T14:00:00-04:00"
},
"start": {
"dateTime": "2016-05-30T13:00:00-04:00"
}
}
我似乎做的一切都是正确的。即使我的 Racket 代码中存在错误,通过 Google 的开发人员 Web 控制台发送完全相同的 JSON 对象似乎也能按预期工作。那么为什么发送这个特定的 POST 不起作用?
关闭。但实际上您在 POST 请求的 headers 字段中遗漏了一条重要数据。即,行:
Content-Type: application/json
将您的整个请求设为:
POST /<Calendar-Id-Redacted> HTTP/1.1
Host: localhost
User-Agent: Racket/6.5.0.5 (net/http-client)
Content-Length: 116
Authorization: Bearer <Key-Redacted>
Content-Type: application/json
{
"end": {
"dateTime": "2016-05-30T14:00:00-04:00"
},
"start": {
"dateTime": "2016-05-30T13:00:00-04:00"
}
}
出于某种原因,如果您缺少它,Google 不会给您一个很好的错误消息,但如果您有它,Google 将跟随 API 调用。
当你 运行 curl 中的命令时,如果你 运行 它像这样(你有一个名为 data.txt
的文件,其中有你的 json object 里面:
curl -X POST -d @data.txt https://www.googleapis.com/calendar/v3/calendars/<Calendar-Id-Redacted>/events --header 'Authorization: Bearer <Key-Redacted>'
它会失败。但是,如果将 header 添加到 --header "Content-Type: application/json"
,API 将按预期工作:
curl -X POST -d @data.txt https://www.googleapis.com/calendar/v3/calendars/<Calendar-Id-Redacted>/events --header 'Authorization: Bearer <Key-Redacted>' --header "Content-Type: application/json"
将 header 添加到您拥有的库中只需要对生成 post 请求的函数进行一项修改:
(define (json-api-post u b
#:token [t #f]
#:headers [headers '("Content-Type: application/json")])
....)
它与您之前的完全相同,只是我们修改了 headers 字段以包含(默认情况下)包含字符串的列表:"Content-Type: application/json"
。通过此更改,google 应该接受您的 API。 (请注意,还有许多其他方法可以修改 headers 函数,这只是一种简单的方法。)
将它们放在一起,您的最终代码应该类似于:
(define (insert-events calendar-id
event
#:token [t #f]
#:max-attendees [max-attendees #f]
#:send-notifications [send-notifications #f]
#:supports-attachments [supports-attachments #f])
(json-api-post (string->url
(format
"https://www.googleapis.com/calendar/v3/calendars/~a/events"
(form-urlencoded-encode calendar-id)))
event
#:token t))
(define (json-api-post u b
#:token [t #f]
#:headers [headers '("Content-Type: application/json")])
(define b* (jsexpr->bytes b))
(let retry ([t t]
[retry-counter 0])
(parse-json-response
(POST-string u b* (if t (cons (token->authorization-header t) headers) headers))
retry
t
retry-counter)))
(define (POST-string u b headers)
(port->string (post-pure-port u b headers)))
希望对您有所帮助。
我正在使用 Google API library for Racket 尝试更新 Google 日历。 (API 不完整,所以我会继续扩展它。)
我似乎无法使用 events.insert put 调用向日历添加事件。执行 put 的代码如下所示:
(define (insert-events calendar-id
event
#:token [t #f]
#:max-attendees [max-attendees #f]
#:send-notifications [send-notifications #f]
#:supports-attachments [supports-attachments #f])
(json-api-post (string->url
(format
"https://www.googleapis.com/calendar/v3/calendars/~a/events"
(form-urlencoded-encode calendar-id)))
event
#:token t))
(define (json-api-post u b
#:token [t #f]
#:headers [headers '()])
(define b* (jsexpr->bytes b))
(let retry ([t t]
[retry-counter 0])
(parse-json-response
(POST-string u b* (if t (cons (token->authorization-header t) headers) headers))
retry
t
retry-counter)))
(define (POST-string u b headers)
(port->string (post-pure-port u b headers)))
但是,无论我如何使用调用,我总是返回错误 400 和消息:"Missing End Time"。我检查了 this question 以确保我正确发送了我的请求。我似乎是。作为参考,我发送的 JSON 对象是:
{
"end": {
"dateTime": "2016-05-30T14:00:00-04:00"
},
"start": {
"dateTime": "2016-05-30T13:00:00-04:00"
}
}
此外,为了确保我正确访问了正确的密钥和日历 ID,我为我的本地计算机设置了一个回显服务器,并将 url 从 google.com
更改为 localhost
,我的回复似乎很正常:
POST /<Calendar-Id-Redacted> HTTP/1.1
Host: localhost
User-Agent: Racket/6.5.0.5 (net/http-client)
Content-Length: 116
Authorization: Bearer <Key-Redacted>
{
"end": {
"dateTime": "2016-05-30T14:00:00-04:00"
},
"start": {
"dateTime": "2016-05-30T13:00:00-04:00"
}
}
我似乎做的一切都是正确的。即使我的 Racket 代码中存在错误,通过 Google 的开发人员 Web 控制台发送完全相同的 JSON 对象似乎也能按预期工作。那么为什么发送这个特定的 POST 不起作用?
关闭。但实际上您在 POST 请求的 headers 字段中遗漏了一条重要数据。即,行:
Content-Type: application/json
将您的整个请求设为:
POST /<Calendar-Id-Redacted> HTTP/1.1
Host: localhost
User-Agent: Racket/6.5.0.5 (net/http-client)
Content-Length: 116
Authorization: Bearer <Key-Redacted>
Content-Type: application/json
{
"end": {
"dateTime": "2016-05-30T14:00:00-04:00"
},
"start": {
"dateTime": "2016-05-30T13:00:00-04:00"
}
}
出于某种原因,如果您缺少它,Google 不会给您一个很好的错误消息,但如果您有它,Google 将跟随 API 调用。
当你 运行 curl 中的命令时,如果你 运行 它像这样(你有一个名为 data.txt
的文件,其中有你的 json object 里面:
curl -X POST -d @data.txt https://www.googleapis.com/calendar/v3/calendars/<Calendar-Id-Redacted>/events --header 'Authorization: Bearer <Key-Redacted>'
它会失败。但是,如果将 header 添加到 --header "Content-Type: application/json"
,API 将按预期工作:
curl -X POST -d @data.txt https://www.googleapis.com/calendar/v3/calendars/<Calendar-Id-Redacted>/events --header 'Authorization: Bearer <Key-Redacted>' --header "Content-Type: application/json"
将 header 添加到您拥有的库中只需要对生成 post 请求的函数进行一项修改:
(define (json-api-post u b
#:token [t #f]
#:headers [headers '("Content-Type: application/json")])
....)
它与您之前的完全相同,只是我们修改了 headers 字段以包含(默认情况下)包含字符串的列表:"Content-Type: application/json"
。通过此更改,google 应该接受您的 API。 (请注意,还有许多其他方法可以修改 headers 函数,这只是一种简单的方法。)
将它们放在一起,您的最终代码应该类似于:
(define (insert-events calendar-id
event
#:token [t #f]
#:max-attendees [max-attendees #f]
#:send-notifications [send-notifications #f]
#:supports-attachments [supports-attachments #f])
(json-api-post (string->url
(format
"https://www.googleapis.com/calendar/v3/calendars/~a/events"
(form-urlencoded-encode calendar-id)))
event
#:token t))
(define (json-api-post u b
#:token [t #f]
#:headers [headers '("Content-Type: application/json")])
(define b* (jsexpr->bytes b))
(let retry ([t t]
[retry-counter 0])
(parse-json-response
(POST-string u b* (if t (cons (token->authorization-header t) headers) headers))
retry
t
retry-counter)))
(define (POST-string u b headers)
(port->string (post-pure-port u b headers)))
希望对您有所帮助。