如何使用objective-c获取Gmail api的访问令牌

如何使用objective-c获取Gmail api的访问令牌,objective-c,oauth-2.0,google-api,gmail,gmail-api,Objective C,Oauth 2.0,Google Api,Gmail,Gmail Api,下面的代码 - (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,

下面的代码

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

   }
}
当我试图输出访问令牌和用户标识时,总是在日志中给我一个“令牌:(null)id:(null)”。然而,授权是正常的。 有谁能告诉我出了什么问题,如何正确获取访问令牌吗?

更改此行:

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

NSLog(@"Token: %@ id: %@", authResult.accessToken, authResult.userID);

您能告诉我在该令牌过期后刷新访问令牌所使用的http方法吗?我可以为相同的令牌使用authResult.refreshToken吗?。这个答案会帮助你的。非常感谢,阿尔维斯!