微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

iOS:Google身份验证代码

我正在使用身份验证用户来使用与之关联的Google帐户.问题是,每次用户通过我的应用程序登录时,“允许访问”始终显示在Google的身份验证视图中,即使我已经从先前的测试中单击了“允许访问”.这是正常的还是我的代码错了?请帮帮我们.

我在out中使用以下代码进行登录

- (IBAction)signIn:(id)sender {
    if(!isSignedIn){
        [self signOutFromAll];

        Nsstring *keychainItemName = nil;

        // save keychain
        keychainItemName = kKeychainItemName;

        Nsstring *scope = @"https://www.googleapis.com/auth/plus.me";

        Nsstring *clientID = kClientID;
        Nsstring *clientSecret = kClientSecret;

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

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

        [[self navigationController]pushViewController:viewController animated:YES]; 
    } else {
        [self displayAlertWithMessage:@"Currently Signed in."];
    } }

- (IBAction)signOut:(id)sender {
    [self signOutFromAll];
    [self displayAlertWithMessage:@"Signed out."]; }

这是代表:

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
      finishedWithAuth:(GTMOAuth2Authentication *)auth 
                 error:(NSError *)error{
    if(error != nil){
        // Authentication Failed...
        NSLog(@"Authentication error: %@",error);
        NSData *responseData = [[error userInfo] objectForKey:@"data"];
        if([responseData length] > 0)
            NSLog(@"%@",[[[Nsstring alloc]initWithData:responseData encoding:NSUTF8StringEncoding]autorelease]);
        self.auth = nil;
    } else {
        // Authentication succeeded...
        isSignedIn = YES;
        self.auth = auth;
    }
}

和awakeFromNib:

- (void)awakeFromNib{
    // Fill in the Client ID and Client Secret text fields
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    // First,we'll try to get the saved Google authentication,if any,from the keychain
    // normal applications will hardcode in their client ID and client secret,// But the sample app allows the user to enter them in a text field,and saves them in the preferences
    Nsstring *clientID      = [defaults stringForKey:kGoogleClientIDKey];
    Nsstring *clientSecret  = [defaults stringForKey:kGoogleClientSecretKey];

    GTMOAuth2Authentication *auth;

    auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                                 clientID:clientID
                                                             clientSecret:clientSecret];

    if (auth.canAuthorize) {
        // There is saved google authentication
        // self.serviceSegments.selectedSegmentIndex = 0;
    } 

    // Save the authentication object,which holds the auth tokens
    self.auth = auth;

    [self setAuth:auth];
    isSignedIn = self.auth.canAuthorize;
}

顺便说一句,我对这些代码的引用是在这链接上:http://code.google.com/p/gtm-oauth2/wiki/Introduction#Using_the_OAuth_2_Controllers

解决方法

来自文档:

The keychain item name is used to save the token on the user’s keychain,and should identify both your application name and the service name(s). If keychainItemName is nil,the token will not be saved,and the user will have to sign in again the next time the application is run.

http://code.google.com/p/gtm-oauth2/wiki/Introduction

因此,从您的代码中,它取决于kKeychainItemName的设置.

我以为在阅读文档时我会对此发表评论.

原文地址:https://www.jb51.cc/iOS/331252.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐