我尝试将事件插入Google日历
下面的代码:
func insertEventToGoogleCalendar() {
Auth.auth().currentUser?.getIDTokenForcingRefresh(true, completion: { (idToken, error) in
if let err = error {
self.unknownError(error: err)
} else {
let documentData: [String: Any] = ["end": ["dateTime": "2019-06-07T17:00:00-07:00"],
"start": ["dateTime": "2019-06-07T17:00:00-07:00"],
"description": "test",
"summary": "test"]
var request = URLRequest(url: URL(string: "https://www.googleapis.com/calendar/v3/calendars/\(CALENDARID)/events")!)
let jsonData = try! JSONSerialization.data(withJSONObject: documentData, options: [])
request.httpMethod = "POST"
request.httpBody = jsonData
request.addValue("Bearer \(idToken!)", forHTTPHeaderField: "Authorization")
request.addValue("application/json", forHTTPHeaderField: "Content-type")
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
if let err = error {
self.unknownError(error: err)
}
if let response = response as? HTTPURLResponse {
print(response.statusCode)
}
if let data = data {
print(String(data: data, encoding: .utf8))
}
})
task.resume()
}
})
}
我缺少什么或代码中缺少什么?我收到了来自Google的错误消息:
Optional("{\n \"error\": {\n \"errors\": [\n {\n \"domain\": \"global\",\n \"reason\": \"authError\",\n \"message\": \"Invalid Credentials\",\n \"locationType\": \"header\",\n \"location\": \"Authorization\"\n }\n ],\n \"code\": 401,\n \"message\": \"Invalid Credentials\"\n }\n}\n")