从ios向Google日历添加活动

时间:2015-07-02 05:34:46

标签: ios calendar google-calendar-api

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

现在我陷入了iOS应用程序的插入事件中,因此它也可以与web同步。请指导我正确的方向或发布一些示例代码。

我在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);
    }
}

但我收到错误回复“操作无法完成。(权限不足)”

谢谢,

3 个答案:

答案 0 :(得分:2)

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

[GTLQueryCalendar queryForEventsInsertWithObject:yourEventObject calendarId:yourCalendarId]

另请注意,您必须使用范围kGTLAuthScopeCalendar进行授权才能具有读/写访问权限。

答案 1 :(得分:0)

我已成功完成这项工作。问题出在您的服务授权人身上。

每次用户登录您的应用时,都会更新accesstoken和refreshtoken,因此您必须创建一个函数,在发送任何类型的请求之前获取授权程序的更新值。

我已经创建了一个函数,可以在每次需要时检索更新的Service Authorizer。

试一试。

干杯。

-(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;

}

答案 2 :(得分:0)

 GTLRCalendar_Event *newEvent = [GTLRCalendar_Event new];

        newEvent.summary = [NSString stringWithFormat:@"%@(%@)", model.listingName, model.listingType];

        newEvent.colorId = @"11";

        newEvent.reminders.useDefault = [NSNumber numberWithInt:1];
  GTLRDateTime *startDateTime = [GTLRDateTime dateTimeWithDate:timingModel.availableTime];

            GTLRCalendar_EventDateTime *startEventDateTime = [GTLRCalendar_EventDateTime new];

            startEventDateTime.dateTime = startDateTime;

            newEvent.start = startEventDateTime;



            GTLRDateTime *endDateTime = [GTLRDateTime dateTimeWithDate:timingModel.closingTime];

            GTLRCalendar_EventDateTime *endEventDateTime = [GTLRCalendar_EventDateTime new];

            endEventDateTime.dateTime = endDateTime;

            newEvent.end = endEventDateTime;



            GTLRCalendarService *service = [GTLRCalendarService new];

            service.authorizer = user.authentication.fetcherAuthorizer;

            GTLRCalendarQuery_EventsInsert *query = [GTLRCalendarQuery_EventsInsert queryWithObject:newEvent calendarId:@"primary"];

            [service executeQuery:query completionHandler:^(GTLRServiceTicket * _Nonnull callbackTicket, id  _Nullable object, NSError * _Nullable callbackError) {

                NSLog(@"%@", @"executed query");

                if(callbackError  == nil)
                {
                    NSLog(@"%@", newEvent.summary);
                }
                else
                {
                    NSLog(@"%@", callbackError);
                }

            }];