我一直在开发一个应用程序,使用Google Calendar API从Google Calendar中检索事件。早在2018年5月,他们在网站上就有一个很好的程序和快速的代码,我在我的应用程序中让它工作。通过这个过程,我能够将Google Calendar中的事件检索到我的应用程序中。现在,我想要在我的应用程序中创建事件或更改事件,并将其导入到Google Calendar中。转到Google Calendar API网站是完全改变的,取而代之的是用于iOS的G Suite API。我在互联网上找不到任何与旧程序相关的东西。使用Google Calendar API的任何人都能提供帮助吗?对于如何使用新事件或事件中的更改更新Google Calendar API有什么想法吗?
发布于 2018-12-31 06:36:24
至于您最初的问题,将事件写入Google Calendar,我可以使用以下函数:
func writetoGC(token:String, startTime: String, endTime: String, summary: String, description: String) {
let url = URL(string: "https://www.googleapis.com/calendar/v3/calendars/{YOUR CALENDAR ID HERE}/events")
let summary1 = confirmationCode + "; " + summary
let session = URLSession.shared
print(session)
var request = NSMutableURLRequest(url: url!)
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
request.httpMethod = "POST"
print(request)
// token, startTime, endTime, summary, description
var myHttpBody: Data = """
{
"end": {
"dateTime": "\(endTime)",
"timeZone": "America/Chicago"
},
"start": {
"dateTime": "\(startTime)",
"timeZone": "America/Chicago"
},
"summary": "\(summary1)",
"description": "\(description)"
}
""".data(using: .utf8)! as Data
do {
request.httpBody = myHttpBody
} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
print("Request: ")
print(request.description)
print(request.allHTTPHeaderFields)
print("Body is:")
print(request.httpBody)
let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in
guard error == nil else {
return
}
guard let data = data else {
return
}
sleep(1)
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("Response is:")
print(json)
print("Description:")
print(json.description)
print("Debug Description:")
print(json.debugDescription)
// handle json...
}
} catch let error {
print("Error during Serialization:")
print(error.localizedDescription)
}
})
task.resume()
//verifyEntry()
}
因此,在这段代码中,除了事件摘要之外,我还传入了一个确认码。确认码是我自己生成的。除非您的摘要中需要一个确认码,否则您不需要这部分代码。如您所见,我已经获得了我的OAuth 2.0令牌,并将其提供给函数。
至于编辑事件,修改上面的代码来更改事件应该不难。
https://stackoverflow.com/questions/51271326
复制相似问题