2016-05-31 8 views
1

RacketGoogle APIライブラリを使用してGoogleカレンダーを更新しようとしています。 (APIが不完全なので、私が行っているように拡張しています)GoogleカレンダーとRacket Googleパッケージの「Missing End Time」

events.insert putコールを使用してカレンダーにイベントを追加することに問題があるようです。 「終了時間がありません」:しかし、関係なく、私が電話を使用する方法、私は常にメッセージとエラー400を取り戻すん

(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))) 

:プットを行うコードは次のようになります。私はthis questionをチェックして、要求を正しく送信していることを確認しました。それは私のように見えます。私はきちんと正しいキーとカレンダーIDをアクセスしていた、私はセットアップまでの私のローカルマシンのechoサーバを確認するために、

{ 
"end": { 
    "dateTime": "2016-05-30T14:00:00-04:00" 
}, 
"start": { 
    "dateTime": "2016-05-30T13:00:00-04:00" 
} 
} 

また、からURLを変更:参考のために、私が送信していますJSONオブジェクトがありますlocalhostgoogle.com、私の応答は、通常のようだ:

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" 
} 
} 

は、私が正しいすべてをやっているようです。私のラケットコードにバグがあったとしても、Googleの開発者向けWebコンソールから正確に同じJSONオブジェクトを送信することは意図した通りに動作するようです。では、なぜこの特定のPOSTを送信しても機能しないのですか?

答えて

3

閉じる。しかし、実際にはPOSTリクエストのヘッダーフィールドに重要なデータが1つ欠落しています。すなわち、ライン:

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はあなたに素敵なエラーメッセージを与えるものではありませんが、あなたは持ってない場合はいくつかの理由:あなたの全体の要求があることを作る

Content-Type: application/json 

それは、GoogleはAPI呼び出しに従います。

あなたはそれであなたのJSONオブジェクトを持っているdata.txtと呼ばれるファイルがある場所(このようにそれを実行する場合は、カールでコマンドを実行するとき、これは、より明確に理解することができます。

curl -X POST -d @data.txt https://www.googleapis.com/calendar/v3/calendars/<Calendar-Id-Redacted>/events --header 'Authorization: Bearer <Key-Redacted>' 

それは失敗します。 。

:ちょうどあなたのポストの要求を生成する関数への1つの変更を必要と

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 "Content-Type: application/json"でヘッダを追加した場合でも、APIは期待通りに動作します

ヘッダーフィールドを変更して(デフォルトで)、文字列:"Content-Type: application/json"を含むリストに変更した点を除いて、これは以前のものとまったく同じです。この変更により、GoogleはAPIを承認します。 (これはちょうどそれを行うための簡単な方法で、ヘッダの機能を変更するには、他の多くの方法があることに注意してください。)

はすべて一緒にそれを置く、あなたの最終的なコードは次のようになります。

(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))) 

希望をそれは助ける。

関連する問題