前提
SwiftでGoogleカレンダーAPIを叩いてイベントを追加したいのですが、GoogleSignInを使ってログインする所まではできたのですが、その先のイベントを追加する所でどのようにリクエストを送れば良いのか分からず困っています。
どうすればイベントを追加できるかご教示いただきたいと思います。
発生している問題・エラーメッセージ
下記のコードの通りだと以下のエラーログのようにmessageが「Bad Request」になるのですが、httpBodyを書かずにリクエストを送るとmessageが「missing end time」となります。ただ、Getメソッドでリクエストを送るとGetなりのレスポンスが返ってくるのでエンドポイントが間違っているわけではなさそうです。
Optional(["error": { code = 400; errors = ( { domain = global; message = "Bad Request"; reason = badRequest; } ); message = "Bad Request"; }])
該当のソースコード
Swift
1import UIKit 2import GoogleSignIn 3 4class ViewController: UIViewController { 5 6 let signInConfig = GIDConfiguration.init(clientID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") 7 8 //サインインボタン 9 @IBAction func signIn(_ sender: AnyObject) { 10 11 GIDSignIn.sharedInstance.signIn(with: signInConfig, presenting: self) { user, error in 12 guard error == nil else { 13 return } 14 // If sign in succeeded, display the app's main content View. 15 16 print("ログイン") 17 let fullName = user?.profile?.name 18 let email = user?.profile?.email 19 20 let driveScopes = [ 21 "https://www.googleapis.com/auth/calendar.readonly", 22 "https://www.googleapis.com/auth/calendar", 23 "https://www.googleapis.com/auth/calendar.events.readonly", 24 "https://www.googleapis.com/auth/calendar.events" 25 ] 26 let grantedScopes = user?.grantedScopes 27 28 if grantedScopes == nil || !grantedScopes!.contains(driveScopes[0]) || !grantedScopes!.contains(driveScopes[1]) || !grantedScopes!.contains(driveScopes[2]) || !grantedScopes!.contains(driveScopes[3]) { 29 // Request additional Drive scope. 30 let additionalScopes = [ 31 "https://www.googleapis.com/auth/calendar.readonly", 32 "https://www.googleapis.com/auth/calendar", 33 "https://www.googleapis.com/auth/calendar.events.readonly", 34 "https://www.googleapis.com/auth/calendar.events" 35 ] 36 GIDSignIn.sharedInstance.addScopes(additionalScopes, presenting: self) { user, error in 37 guard error == nil else { return } 38 guard let user = user else { return } 39 40 // Check if the user granted access to the scopes you requested. 41 getEvents(token: user.authentication.accessToken) 42 } 43 } else { 44 getEvents(token: user!.authentication.accessToken) 45 } 46 } 47 } 48 49 //サインアウトボタン 50 @IBAction func signOut(_ sender: AnyObject) { 51 GIDSignIn.sharedInstance.signOut() 52 print("ログアウト") 53 } 54} 55 56private func getEvents(token: String) { 57 58 var urlComponents = URLComponents(string: "https://www.googleapis.com/calendar/v3/calendars/xxxxxxxxx(カレンダーID)/events") 59 urlComponents?.queryItems = [ 60 URLQueryItem(name: "calendarId", value: "primary") // 例) emailとpasswordを付与 61 ] 62 let config = URLSessionConfiguration.default 63 let session = URLSession(configuration: config) 64 var request = URLRequest(url: (urlComponents?.url)!) 65 request.httpMethod = "POST" 66 request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization") 67 68//==================================================== 69//そもそもここ以外に問題があるのかも知れませんが、現時点ではここの正しい 70//書き方が分からないためにうまく行っていないと思っています。 71 72 request.httpBody = "end=datetime:2022-03-27T00:00:00+09:00&timeZone:Asia/Tokyo".data(using: .utf8) 73 74//================================================ 75 76 let task = session.dataTask(with: request) { (data, response, error) in 77 if let data = data { 78 let json = try? (JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]) 79 print(json) 80 } 81 } 82 task.resume() 83}
試したこと
GoogleDevelopersのサイトを見ながらコードを書いたのですが、Swiftの例文が無いので他の言語の例文を見ても何をやっているのか想像するしかありませんでした。
「Swift Google CalendarAPI イベント 追加」など色々な言葉を組み合わせて検索し、片っ端から例文を試したのですが、エラーや警告に阻まれてどれもうまく行きませんでした。
ディベロッパーサイトの「Try it now」も試したのですが、そもそもどんな内容でリクエストを送れば事足りるのかも分からないため、色々試しましたが様々なエラーが返ってくるだけでした。
どんな内容をどのようにリクエストとして送れば正しくレスポンスを得られるのか、教えていただけると助かります。よろしくお願いします。
補足情報(FW/ツールのバージョンなど)
Xcode11.6
iPhoneSE
iOS 13.6
回答1件
あなたの回答
tips
プレビュー