问题描述
我想要做的是从同步谷歌日历到我的应用程序中的数据。
What I want to do is sync the data from google calendar to my app.
我可以登录我与谷歌的应用程序+帐户现在,我又如何能得到日历私人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?
先谢谢了。
推荐答案
我可以检索使用的。作为一个例子,这里是一个code片段进行检索我的日历元数据的请求:
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()
至于您的应用程序和谷歌日历之间进行同步的数据,可以实现回调,使PUT请求通过谷歌Calendar API更新。作为一个例子,这里是一个code段的回调函数,使PUT请求同步瓦特/你的谷歌日历:
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
}
}
}
有很多可用的雨燕HTTP包装库。在我的例子中,我使用。
There are numerous Swift HTTP wrapper libraries available. In my example, I am using Swift HTTP.
这篇关于如何从谷歌日历API JSON在迅速?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!