简体   繁体   English

适用于 iOS Swift 的 Google 日历 API

[英]Google Calendar API for iOS Swift

I have been working on an app to retrieve events from a Google Calendar using the Google Calendar API.我一直在开发一个使用 Google 日历 API 从 Google 日历中检索事件的应用程序。 They had a nice procedure and swift code on the web site back in May 2018 and I got it working in my app.早在 2018 年 5 月,他们就在网站上提供了一个很好的程序和快速代码,我让它在我的应用程序中运行。 With the procedure, I am able to retrieve the events from the Google Calendar into my app.通过该程序,我可以将 Google 日历中的事件检索到我的应用程序中。 Now, I want to create an event or change an event in my app and have it imported into the Google Calendar.现在,我想在我的应用程序中创建一个事件或更改一个事件并将其导入到 Google 日历中。 Went to the Google Calendar API web site is completely changed and is replaced with G Suite APIs for iOS.转到 Google Calendar API 网站已完全更改并替换为适用于 iOS 的 G Suite API。 I am unable to find anything related on the internet related to the old procedure.我无法在互联网上找到与旧程序相关的任何内容。 Can anyone working with Google Calendar API help?任何使用 Google Calendar API 的人都可以提供帮助吗? Any ideas on how to update Google Calendar API with a new event or a change in an event?关于如何使用新事件或事件更改更新 Google Calendar API 的任何想法?

As for your original question, writing events to the Google Calendar, I was able to use the following function:至于你原来的问题,将事件写入谷歌日历,我能够使用以下功能:

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()
}

So in this code I'm passing in a confirmation code in addition to the summary of the events.所以在这段代码中,除了事件摘要之外,我还传递了一个确认代码。 The confirmation code is one that I generate myself.确认码是我自己生成的。 You won't need that portion of the code unless you are needing a confirmation code in your summary.除非您在摘要中需要确认代码,否则您不需要该部分代码。 As you can see, I've already gained my OAuth 2.0 token and fed it into the function.如您所见,我已经获得了我的 OAuth 2.0 令牌并将其输入到函数中。

As for editing events, it shouldn't be hard to modify the code above to change an event.至于编辑事件,修改上面的代码来改变事件应该不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM