GTMOAuth2Authentication Not Working

680 Views Asked by At

I'm trying to use Google's OAuth2 and YouTube APIs. The OAuth returns GTMOAuth2Authentication object that you then use to make requests to services like YouTube. My login works fine, and when I manually pass the authentication object, I can make requests.

However, I should also be able to access the authentication object via keychain, and I receive a valid object, but if I try to use it to make requests, I cannot. I keep getting the following error: "The operation couldn’t be completed. (com.google.GTMHTTPFetcher error -1.)" I'd appreciate it if someone could point out my mistake. I'm testing on a real iPhone 5s.

Authentication Code:

GTMOAuth2ViewControllerTouch * viewController = [[GTMOAuth2ViewControllerTouch alloc]
                                                 initWithScope:scope
                                                 clientID:kGoogleClientID
                                                 clientSecret:kGoogleClientSecret
                                                 keychainItemName:kGoogleKeychainItemName
                                                 delegate:self
                                                 finishedSelector:@selector(viewController:
                                                                            finishedWithAuth:
                                                                            error:)];
[self.navigationController pushViewController:viewController animated:YES];

Authentication Completion Handler:

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error {

    NSLog(@"%s", __PRETTY_FUNCTION__);

    if (error != nil) {
        NSLog(@"%s %@", __PRETTY_FUNCTION__, error.localizedDescription);
        return;
    }

    MediaGETWrapper *getWrapper = [MediaGETWrapper sharedWrapper];
    getWrapper.googleAuth = auth; // passing auth directly without keychain

    [getWrapper youTubeSubscriptionsWithSuccess:nil failure:nil];

YouTube Client Initialization:

self.youTube = [GTLServiceYouTube new];
GTMOAuth2Authentication *auth = [GTMOAuth2Authentication new];

[GTMOAuth2ViewControllerTouch
 authorizeFromKeychainForName:kGoogleKeychainItemName
 authentication:auth
 error:nil];

self.youTube.authorizer = auth;

Request:

- (void)youTubeSubscriptionsWithSuccess:(void(^)(NSArray *subscriptions))success
                                failure:(void(^)(NSError *error))error {

    NSLog(@"%s", __PRETTY_FUNCTION__);

//    self.youTube.authorizer = self.googleAuth; // If uncommented, works!

    GTLQueryYouTube *query = [GTLQueryYouTube queryForSubscriptionsListWithPart:@"snippet"];
    query.mine = YES;

    [self.youTube
     executeQuery:query
     completionHandler:^(GTLServiceTicket *ticket,
                         GTLYouTubeChannelListResponse *channelList,
                         NSError *error) {

        if (error != nil) {
            NSLog(@"%s %@", __PRETTY_FUNCTION__, error.localizedDescription); // fails here
            return;
        }

        for (GTLYouTubeSubscription *channel in channelList) {
            NSLog(@"%@", channel.snippet.title);
        }
     }];
}
1

There are 1 best solutions below

0
Impossibility On

I couldn't find a direct fix, but you can do this:

Get the access token from a GTMOAuth2Authentication object via:

auth.accessToken

Then set the access token wherever you want to make requests. To refresh the token, use this method:

[auth 
 authorizeRequest:nil // just to refresh
 completionHandler:^(NSError *error) {
     // your code here
 }];