小编hjo*_*ard的帖子

如何在iOS中使用GIDSignIn和GTMOAuth2Authentication获取刷新令牌?

我正在编写一个iOS应用程序,它使用Google的GIDSignIn [1]登录用户,GTLServiceYoutube对Youtube执行查询(上传视频和检索Youtube视频列表).

这在用户首次登录时工作正常,但大约一小时后,访问令牌过期,用户由于401错误(凭据无效)而无法再使用GTLServiceYoutube执行查询.

成功登录后,我使用以下代码设置GTMOAuth2Authentication:

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {

    if (error == nil) {
        [self setAuthorizerForSignIn:signIn user:user];
    }
    [super signIn:signIn didSignInForUser:user withError:error];
}

- (void)setAuthorizerForSignIn:(GIDSignIn *)signIn user:(GIDGoogleUser *)user {
     GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init];

    [auth setClientID:signIn.clientID];
    [auth setClientSecret:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"GoogleClientSecret"]];
    [auth setUserEmail:user.profile.email];
    [auth setUserID:user.userID];
    [auth setAccessToken:user.authentication.accessToken];
    [auth setRefreshToken:user.authentication.refreshToken];
    [auth setExpirationDate: user.authentication.accessTokenExpirationDate];
    [[UserManager sharedInstance].youTubeService setAuthorizer:auth];
}
Run Code Online (Sandbox Code Playgroud)

哪里[[UserManager sharedInstance].youTubeService是GTLServiceYouTube的一个实例.

唯一的问题是GTLServiceYouTube.GIDSignIn似乎处理刷新令牌,因此用户始终在首次登录后登录.但GTLOAuth2Authentication仅在首次登录时有效,并在一小时后中断.

所以我的问题是:我在这里做错了吗?或者我在刷新后遗漏了一些东西以获得GTMOAuth2Authentication中的正确访问令牌?

[1] https://developers.google.com/identity/sign-in/ios/api/interface_g_i_d_sign_in

youtube-api ios oauth-2.0 google-plus

8
推荐指数
1
解决办法
3977
查看次数

标签 统计

google-plus ×1

ios ×1

oauth-2.0 ×1

youtube-api ×1