I am using the gdata sdk to upload a video to youtube. Today I was testing what would happen if I revoke the access of my app to youtube. After revoking access in Google Account security(https://accounts.google.com/b/0/IssuedAuthSubTokens), I tried to upload a video to youtube. Strangely my finished selector wasn't being called neither with error or success. This made me believe that I should somehow check more thorough if I have permission to upload to youtube.

The problem is I don't know how this should be done. This is my current code to check if I need to perform a sign in:

    - (BOOL)isSignedIn
    {
        NSString *clientID = @"CLIENT_ID";
        NSString *clientSecret = @"CLIENT_SECRET";
        NSString *keychainName= @"KEYCHAIN_NAME";

        GTMOAuth2Authentication *auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:keychainName
                                                                     clientID:clientID
clientSecret:clientSecret];

        [[self youTubeService] setAuthorizer:auth];
        return  auth.canAuthorize;

    }

This is the code that signs in if isSignedIn returns NO:

-(void)performSignIn:(id)arg
{       
    NSString *scope = [GDataServiceGoogleYouTube authorizationScope];

    NSString *clientID = @"CLIENT_ID";
    NSString *clientSecret = @"CLIENT_SECRET";
    NSString *keychainName= @"KEYCHAIN_NAME";

    SEL finishedSel = @selector(viewController:finishedWithAuth:error:);




    GTMOAuth2ViewControllerTouch *viewController;
    viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
                                                              clientID:clientID
                                                          clientSecret:clientSecret
                                                      keychainItemName:keychainName
                                                              delegate:self
                                                      finishedSelector:finishedSel];

    UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [self presentModalViewController:aNavController animated:YES];
    [aNavController release];

}

Is there a way to check if my access token is still valid?

有帮助吗?

解决方案

In general, you can check whether a Google-issued OAuth 2 access token is valid by calling https://www.google.com/accounts/AuthSubTokenInfo?bearer_token=OAUTH_2_TOKEN and see whether you get back a HTTP 200 or 40x response.

What makes more sense is to do a "cheap" authenticated YouTube API call, like for https://gdata.youtube.com/feeds/api/users/default?v=2, and see if you get back a valid response or an error.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top