【问题标题】:How to get access token for Gmail api using objective-c如何使用objective-c获取Gmail api的访问令牌
【发布时间】:2016-12-13 15:36:13
【问题描述】:

以下代码

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

   }
}

当我尝试输出访问令牌和用户 ID 时,总是在日志中给我一个“令牌:(null) id:(null)”。但是,授权很好。 谁能告诉我出了什么问题,如何正确获取访问令牌?

【问题讨论】:

    标签: objective-c oauth-2.0 google-api gmail gmail-api


    【解决方案1】:

    改变这一行:

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

    进入这个:

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

    【讨论】:

    • 你能告诉我这个令牌过期后我可以使用哪种http方法刷新访问令牌吗?
    • 我可以使用 authResult.refreshToken 吗?
    • stackoverflow.com/a/21934196/2640210。这个答案会对你有所帮助。
    • 非常感谢,阿尔维斯!
    猜你喜欢
    • 2016-11-30
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2020-03-09
    • 2016-12-27
    • 2012-04-11
    • 2020-11-04
    相关资源
    最近更新 更多