1

The following code

- (void)viewDidLoad {
[super viewDidLoad];

// Create a UITextView to display output.
self.output = [[UITextView alloc] initWithFrame:self.view.bounds];
self.output.editable = false;
self.output.contentInset = UIEdgeInsetsMake(20.0, 0.0, 20.0, 0.0);
self.output.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.output];

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

[GIDSignIn sharedInstance].clientID = kClientID;
[[GIDSignIn sharedInstance] signInSilently];
NSLog(@"token: %@", [GIDSignIn sharedInstance].currentUser.authentication.accessToken);
}

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
  finishedWithAuth:(GTMOAuth2Authentication *)authResult
             error:(NSError *)error {
if (error != nil) {
    [self showAlert:@"Authentication Error" message:error.localizedDescription];
    self.service.authorizer = nil;
}
else {
    self.service.authorizer = authResult;
    NSLog(@"Token: %@ id: %@", [GIDSignIn sharedInstance].currentUser.authentication.accessToken, [GIDSignIn sharedInstance].currentUser.userID);
    [self dismissViewControllerAnimated:YES completion:nil];

   }
}

Always gives me a ' token: (null) id: (null)' in logs when I try to output the access token and userID. However, authorization goes fine. Could anybody tell me what's wrong, how to get the access token correctly?

1 Answer 1

2

Change this line:

NSLog(@"Token: %@ id: %@", [GIDSignIn sharedInstance].currentUser.authentication.accessToken, [GIDSignIn sharedInstance].currentUser.userID);

into this:

NSLog(@"Token: %@ id: %@", authResult.accessToken, authResult.userID);
Sign up to request clarification or add additional context in comments.

4 Comments

Could you please tell me the http method using which I can refresh access token after this token expired?
May I use authResult.refreshToken for the same?
stackoverflow.com/a/21934196/2640210. This answer will help you.
Thanks a lot, Alvis!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.