iOS:谷歌验证码

发布于 2025-01-04 10:21:35 字数 3623 浏览 1 评论 0原文

我正在与验证用户身份以使用与他关联的谷歌帐户。问题是,每次用户通过我的应用程序登录时,“允许访问”总是出现在 Google 的身份验证视图上,即使我在之前的测试中已经单击了“允许访问”。这是正常现象还是我的代码写错了?请帮助我。

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

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

I am working with authenticating user to use the google account he is associated with. The problem is that everytime the user logs in through my app, the "Allow Access" always appears on the Google's authentication view even I had clicked the Allow Access already from previous test. Is this normal or am I doing my codes wrong? Please help me guys.

I used the following codes for loggin in an 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."]; }

This is for the delegate:

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

And 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;
}

By the way my reference for these codes is on this link: http://code.google.com/p/gtm-oauth2/wiki/Introduction#Using_the_OAuth_2_Controllers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

空气里的味道 2025-01-11 10:21:35

来自文档:

钥匙串项名称用于将令牌保存在用户的钥匙串上,并且应标识您的应用程序名称和服务名称。如果 keychainItemName 为 nil,则不会保存令牌,用户下次运行应用程序时必须重新登录。

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

因此,从您的代码来看,这取决于 kKeychainItemName 的设置。

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

from the docs:

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

So, from your code, it depends on what kKeychainItemName is set to.

Just thought I'd comment on this as I was reading the docs.

木落 2025-01-11 10:21:35

当您将 oauth 对象保存到钥匙串中时,请使用此方法

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth];

,并且

在调用 api 之前,只需使用此方法检查并检索 oauth 对象

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
                                      authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME
                                      clientID:GOOGLE_CLIENT_KEY
                                      clientSecret:GOOGLE_CLIENT_SECRET];

,并使用此方法确保它的 oauth 对象是真实的

if(![GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth])

Use this method when you get the oauth object to save into keychain

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth];

and

before making a call to api just check and retrieve the oauth object using this

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
                                      authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME
                                      clientID:GOOGLE_CLIENT_KEY
                                      clientSecret:GOOGLE_CLIENT_SECRET];

and make sure it's oauth object is authentic with using this

if(![GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth])
一场信仰旅途 2025-01-11 10:21:35

我知道这是一个老问题,但我遇到了同样的问题,所以我正在编写我的解决方案,它可能会在将来帮助其他人。

事实证明,仅设置 self.auth 是不够的,您还需要设置 self.analyticsService.authorizer 变量

if ([self.auth canAuthorize])
{
    self.analyticsService.authorizer = self.auth;
    [self getAnalyticsData];
    return;
}

这对我来说是成功的,用户不再是要求输入凭据。

I know this is an old question, but I encountered the same issue so I'm writing my solution, it might help somebody else in the future.

Turns out it's not sufficient to only set self.auth, you also need to set the self.analyticsService.authorizer variable

if ([self.auth canAuthorize])
{
    self.analyticsService.authorizer = self.auth;
    [self getAnalyticsData];
    return;
}

This did the trick for me, the user is no longer asked to enter the credentials.

ゞ记忆︶ㄣ 2025-01-11 10:21:35
Put the below code to logout / sign out from Google SDK.

- Call below function from where you want:

static NSString *const kKeychainItemName = @"MY_APP";



- (void)logoutFromGoogleDrive {

[GTMOAuth2SignIn revokeTokenForGoogleAuthentication:(GTMOAuth2Authentication *)self.driveService.authorizer];

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:kKeychainItemName authentication:nil];

}

[Note: Above code works, if you have used GTMOAuth2SignIn  for sign in user for google access like,

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME
clientID:GOOGLE_CLIENT_KEY
clientSecret:GOOGLE_CLIENT_SECRET];

]
Put the below code to logout / sign out from Google SDK.

- Call below function from where you want:

static NSString *const kKeychainItemName = @"MY_APP";



- (void)logoutFromGoogleDrive {

[GTMOAuth2SignIn revokeTokenForGoogleAuthentication:(GTMOAuth2Authentication *)self.driveService.authorizer];

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:kKeychainItemName authentication:nil];

}

[Note: Above code works, if you have used GTMOAuth2SignIn  for sign in user for google access like,

GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME
clientID:GOOGLE_CLIENT_KEY
clientSecret:GOOGLE_CLIENT_SECRET];

]
失而复得 2025-01-11 10:21:35

根据我的经验,这种行为是正常的。

您是否有疑问,因为 Facebook 仅询问用户一次,如果用户想要授予应用程序访问用户个人资料的权限?

From my experience, this behavior is normal.

Are you having doubts because facebook only asks the user once if the user wants to grant the app privileges to access the user's profile?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文