问题描述
我想做的是将谷歌日历中的数据同步到我的应用中.
What I want to do is sync the data from google calendar to my app.
我现在可以使用 google+ 帐户登录我的应用程序,那么如何使用我的帐户 ID 和密码从日历中获取私有 JSON 数据?
I can login my app with google+ account now, then how can I get the private JSON data from calendar with my account ID and password?
提前致谢.
推荐答案
我能够使用 Google 日历 API.例如,这里是一个代码片段,用于请求检索我的日历元数据:
I was able to retrieve JSON data via HTTP requests using Google Calendar API. As an example, here is a code snippet to make a request to retrieve my calendar metadata:
let url = NSURL(string: "https://www.googleapis.com/calendar/v3/calendars/calendarId")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
let dataAsNSString = NSString(data: data, encoding: NSUTF8StringEncoding)
// implement your app logic
}
task.resume()
至于在您的应用和 Google 日历之间同步数据,您可以实现回调以通过 Google 日历 API 发出更新 PUT 请求.例如,这里是一个回调函数的代码片段,用于发出 PUT 请求以与您的 Google 日历同步:
As far as syncing data between your app and Google Calendar, you can implement callbacks to make PUT requests to update via Google Calendar API. As an example, here is a code snippet for a callback function to make a PUT request to sync w/ your Google Calendar:
func updateUserDataToGoogleCalendar(calendarId: String) {
let url = NSURL(string:"https://www.googleapis.com/calendar/v3/calendars/calendarId")
let dataAsString = "sync data"
var data: NSData? = dataAsString.dataUsingEncoding(NSUTF8StringEncoding)
var headers = Dictionary<String, String>() // specify any headers
Http().put(url!, headers: headers, data:data!) { (result) in
if result.success {
if let json: AnyObject = result.jsonObject {
// implement your app logic
}
}
}
有许多可用的 Swift HTTP 包装器库.在我的示例中,我使用 Swift HTTP.
There are numerous Swift HTTP wrapper libraries available. In my example, I am using Swift HTTP.
这篇关于如何快速从谷歌日历 API 获取 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!