iOS:谷歌验证码

标签 ios iphone gmail google-authentication

我正在对用户进行身份验证,以使用与他关联的 Google 帐户。问题是每次用户通过我的应用程序登录时,“允许访问”总是出现在 Google 的身份验证 View 中,即使我已经在之前的测试中点击了允许访问。这是正常现象还是我的代码有误?请帮帮我。

我使用以下代码登录和退出:

- (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 的设置。

只是想在阅读文档时对此发表评论。

关于iOS:谷歌验证码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9224309/

相关文章:

ios - 如何在ios应用程序中包含皮肤

iphone - 在 Xcode iPhone 项目中检测我是否正在为模拟器或设备构建?

iphone - 为原始数据类型堆栈编写方法时使用 'id' 吗?

gmail - GMail 如何显示侧边栏?

ios - 从另一个路径的线宽轮廓生成CGPath

iphone - 如何控制推送通知中按钮的作用?

ios - objective-c 在发送到 web 服务后卡住

iphone - MPMoviePlayerController 不工作,也没有通知

.net - 带有 .NET Core 应用程序的 Google API

php - Gmail 和 PHP MAIL 函数