0

我正在使用 Tornado 网络,我正在尝试使用 httpclient 发出 http 请求以将事件插入 Google 日历。但我总是收到 400 错误请求错误。你能帮我解决这个问题吗?谢谢!

    event = {
         'start': {
                  'date': '2012-07-11'
                  },
         'end':   {
                  'date': '2012-07-12'
                  },
            }
    body = urllib.urlencode(event)
    http_client = tornado.httpclient.AsyncHTTPClient()
        headers = {'Authorization':'Bearer '+access_token}
        req =   tornado.httpclient.HTTPRequest(url="https://www.googleapis.com/calendar/v3/calendars/primary/events?key="+self.settings["google_developer_key"],
                                         method="POST",
                                         body=body,
                                         headers=headers)
        http_client.fetch(req, callback=self.insert_event_response)

这是我得到的错误:

    HTTPResponse(code=400,request_time=None,buffer=<io.BytesIO object at     0x1bb3470>,_body=None,time_info={},request=<tornado.httpclient.HTTPRequest object at 0x1ccc310>,effective_url='https://www.googleapis.com/calendar/v3/calendars/primary/events?key=AIzaSyAyy9M1HZ1nDMdBwGMPDLamhFkCB8iQEJ0',headers={'X-Xss-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Transfer-Encoding': 'chunked', 'Expires': 'Wed, 11 Jul 2012 03:19:29 GMT', 'Server': 'GSE', 'Cache-Control': 'private, max-age=0', 'Date': 'Wed, 11 Jul 2012 03:19:29 GMT', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Type': 'application/json; charset=UTF-8'},error=HTTPError('HTTP 400: Bad Request',))
4

2 回答 2

1

最后让它工作,标题需要:

    headers = {'Authorization':'Bearer '+access_token, 'X-JavaScript-User-Agent':  'Google       APIs Explorer', 'Content-Type':  'application/json'}
于 2012-07-11T20:15:16.797 回答
0

你确定身体必须urllib编码而不是json编码吗?

于 2012-07-11T15:11:59.570 回答