从ios添加活动到谷歌日历

我已经按照以下教程来获取从谷歌日历这是工作正常的事件。 https://developers.google.com/google-apps/calendar/quickstart/ios

现在我被困在从我的iOS应用程序插入事件,以便它可以与networking同步。 请引导我在正确的方向或张贴一些示例代码。

我在viewDidLoad中使用这个代码进行授权

// Initialize the Google Calendar API service & load existing credentials from the keychain if available. self.service = [[GTLServiceCalendar alloc] init]; self.service.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kClientID clientSecret:kClientSecret]; 

授权似乎很好,因为获取事件工作得很好。 但是我正在使用下面的代码来添加一个事件

 - (void)addAnEvent { // Make a new event, and show it to the user to edit GTLCalendarEvent *newEvent = [GTLCalendarEvent object]; newEvent.summary = @"Sample Added Event"; newEvent.descriptionProperty = @"Description of sample added event"; // We'll set the start time to now, and the end time to an hour from now, // with a reminder 10 minutes before NSDate *anHourFromNow = [NSDate dateWithTimeIntervalSinceNow:60*60]; GTLDateTime *startDateTime = [GTLDateTime dateTimeWithDate:[NSDate date] timeZone:[NSTimeZone systemTimeZone]]; GTLDateTime *endDateTime = [GTLDateTime dateTimeWithDate:anHourFromNow timeZone:[NSTimeZone systemTimeZone]]; newEvent.start = [GTLCalendarEventDateTime object]; newEvent.start.dateTime = startDateTime; newEvent.end = [GTLCalendarEventDateTime object]; newEvent.end.dateTime = endDateTime; GTLCalendarEventReminder *reminder = [GTLCalendarEventReminder object]; reminder.minutes = [NSNumber numberWithInteger:10]; reminder.method = @"email"; newEvent.reminders = [GTLCalendarEventReminders object]; newEvent.reminders.overrides = [NSArray arrayWithObject:reminder]; newEvent.reminders.useDefault = [NSNumber numberWithBool:NO]; [self addEvent:newEvent]; } - (void)addEvent:(GTLCalendarEvent *)event { GTLQueryCalendar *query = [GTLQueryCalendar queryForEventsInsertWithObject:event calendarId:@"primary"]; [self.service executeQuery:query delegate:self didFinishSelector:@selector(displayAddEventResultWithTicket:finishedWithObject:error:)]; } - (void)displayAddEventResultWithTicket:(GTLServiceTicket *)ticket finishedWithObject:(GTLCalendarEvents *)events error:(NSError *)error { if (error == nil) { NSLog(@"I think event has been added successfully!"); } else { NSLog(@"ERROR : %@", error.localizedDescription); } } 

但是我得到了错误的回应“操作无法完成(权限不足)”

谢谢,

要将事件添加到日历,请使用以下方法

 [GTLQueryCalendar queryForEventsInsertWithObject:yourEventObject calendarId:yourCalendarId] 

另外请注意,您必须授权使用范围kGTLAuthScopeCalendar具有读取/写入权限。

我设法做这个工作。 问题出在您的服务授权人员身上。

每当用户login到应用程序时,accesstoken和refreshtoken都会更新,因此您必须创build一个函数,在发送任何types的请求之前获取授权人的更新值。

我创build了一个函数来检索更新的服务授权器,每次我需要它。

试一试。

干杯。

 -(GTLServiceCalendar *) updateAuthService{ GTLServiceCalendar *service = [GTLServiceCalendar new]; GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init]; [auth setClientID:kClientID]; [auth setClientSecret:kClientSecret]; [auth setUserEmail:[GIDSignIn sharedInstance].currentUser.profile.email]; [auth setUserID:[GIDSignIn sharedInstance].currentUser.userID]; [auth setAccessToken:[GIDSignIn sharedInstance].currentUser.authentication.accessToken]; [auth setRefreshToken:[GIDSignIn sharedInstance].currentUser.authentication.refreshToken]; [auth setExpirationDate: [GIDSignIn sharedInstance].currentUser.authentication.accessTokenExpirationDate]; service.authorizer = auth; return service; }