0

I am trying to add events to google calendar but in ios 10 i am getting following error even i login my gmail account in settings --> calendar.enter image description here

I am using the following code

    GTLServiceCalendar *_calendarService = [[GTLServiceCalendar alloc] init];

    _calendarService.authorizer = [GTMOAuth2ViewControllerTouch
                                   authForGoogleFromKeychainForName:kGoogleAPIKeychainItemName
                                   clientID:kGoogleAPIClientID
                                   clientSecret:nil];

    if (!_calendarService.authorizer.canAuthorize)
        //if([auth canAuthorize])
    {
        [self launchGoogleAuthenticationView];
    }
    else {


        [self addEventToGoogleCalendar];
    }
  - (void)launchGoogleAuthenticationView {
_didCancelGoogleAuthentication = NO;

GTMOAuth2ViewControllerTouch *authController;

// If modifying these scopes, delete your previously saved credentials by
// resetting the iOS simulator or uninstall the app.
NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeCalendar, nil];

authController = [[GTMOAuth2ViewControllerTouch alloc]
                  initWithScope:[scopes componentsJoinedByString:@" "]
                  clientID:kGoogleAPIClientID
                  clientSecret:nil
                  keychainItemName:kGoogleAPIKeychainItemName
                  delegate:self
                  finishedSelector:@selector(googleAuthenticationViewController:finishedWithAuth:error:)];

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];

[closeButton setTitle:@"Cancel" forState:UIControlStateNormal];

[closeButton setBackgroundColor:appredcolor];

[closeButton addTarget:self
                action:@selector(didTapCloseButton:)
      forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *closeButtonItem = [[UIBarButtonItem alloc]
                                    initWithCustomView:closeButton];

[closeButtonItem setTintColor:appredcolor];

[authController.navigationItem setLeftBarButtonItem:closeButtonItem];

UINavigationController *navController = [[UINavigationController alloc]
                                         initWithRootViewController:authController];

 [myappDelegate.navCont.viewControllers.lastObject presentViewController:navController animated:YES completion:nil];
 }
- (void)addEventToGoogleCalendar {

NSString *appdatestr =[[CTManager sharedInstance] getAppointmentForId:cardict.carsIdentifier];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"dd-MMM-yy, hh:mm a"];

_calendarEvent = [[GTLCalendarEvent alloc] init];

[_calendarEvent setSummary:@"Fathik"];
[_calendarEvent setDescriptionProperty:@"Adding event in UCR"];

NSDate *startDate = [outputFormatter dateFromString:appdatestr];
NSDate *endDate = [startDate dateByAddingTimeInterval:60*60];

if (endDate == nil) {
    endDate = [startDate dateByAddingTimeInterval:(60 * 60)];
}

GTLDateTime *startTime = [GTLDateTime dateTimeWithDate:startDate
                                              timeZone:[NSTimeZone systemTimeZone]];

[_calendarEvent setStart:[GTLCalendarEventDateTime object]];
[_calendarEvent.start setDateTime:startTime];

GTLDateTime *endTime = [GTLDateTime dateTimeWithDate:endDate
                                            timeZone:[NSTimeZone systemTimeZone]];

[_calendarEvent setEnd:[GTLCalendarEventDateTime object]];
[_calendarEvent.end setDateTime:endTime];


GTLQueryCalendar *insertQuery = [GTLQueryCalendar queryForEventsInsertWithObject:_calendarEvent
                                                                      calendarId:kGoogleAPICalendarID];
//[self showAlertWithTitle:nil
//     andMessage:NSLocalizedString(@"Adding Event…", nil)];

[_calendarService executeQuery:insertQuery
             completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
                 if (error == nil) {
                     NSLog(@"event added");
                     [myappDelegate.navCont.view makeToast:@"Event added to your Calender"
                                                  duration:2.0
                                                  position:CSToastPositionBottom
                                                     title:nil];
                 } else {
                     NSLog(@"event added failed --- %@",[error description]);
                 }
             }];


}

Please suggest any idea.Thanks in advance

G.S.Koti
  • 66
  • 6
  • 1
    answered at http://stackoverflow.com/questions/40591090/403-error-thats-an-error-error-disallowed-useragent – Jen Jose Feb 07 '17 at 18:02

2 Answers2

1

Add this few line of code in your viewDidLoad() I was facing same issue, After a great pursuing the problem I got the following given solution. *Thanks For Mr. Adnan Ameen*

// set default useragent

NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:@"Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341", @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
0

Google has updated its security restrictions for OAuth flow. They are not going to allow native web-views to initiate OAuth flows, but rather are encouraging people to use the OS browsers to do so. In your case, you'll probably have to wait for the Google calendar SDK to update their code to obey the newly recommended flow. More information is available in the Google blog

Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30