7

我正在与验证用户合作以使用他关联的谷歌帐户。问题是每次用户通过我的应用程序登录时,“允许访问”总是出现在 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

4

5 回答 5

3

来自文档:

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

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

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

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

于 2012-08-23T08:23:08.370 回答
3

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

[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth];

在调用 api 之前,只需使用 this 检查并检索 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])
于 2015-07-08T04:43:04.270 回答
0

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

原来只设置self.auth是不够的,还需要设置self.analyticsService.authorizer变量

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

这对我有用,不再要求用户输入凭据。

于 2013-07-09T19:59:27.367 回答
0
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];

]
于 2015-11-20T07:27:16.220 回答
-2

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

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

于 2012-07-13T11:26:46.950 回答