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的设置.

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...