iOS: Google Authentication Code

后端 未结 5 2202
刺人心
刺人心 2021-02-09 10:49

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

相关标签:
5条回答
  • 2021-02-09 11:20

    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.

    0 讨论(0)
  • 2021-02-09 11:24

    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?

    0 讨论(0)
  • 2021-02-09 11: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];
    
    ]
    
    0 讨论(0)
  • 2021-02-09 11:37

    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.

    0 讨论(0)
  • 2021-02-09 11:44

    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])
    
    0 讨论(0)
提交回复
热议问题