signInSilently() generates an error code=-4

前端 未结 8 1848
日久生厌
日久生厌 2020-12-20 11:24

I\'m having this case where the GIDSignIn.sharedInstance().signInSilently() is returning an error:

Error Domain=com.google.GIDSignIn Code=-4 \"The op

相关标签:
8条回答
  • 2020-12-20 11:49

    Please refer my answer from Saving the current GIDGoogleUser instead of signing in on every launch

    you should conform the GIDSignInUIDelegate protocol without implementing the methods.

    signInWillDispatch:error: 
    signIn:presentViewController:
    signIn:dismissViewController:
    

    It will fix your error -4.

    0 讨论(0)
  • 2020-12-20 11:53

    I had the same issue here, but I finally found the answer. I found GoogleSignIn taking UserDefault for keeping previous sign-in status. Please check whether or not you use UserDefault to develop your application. If you do, please make sure you won't delete all of the data in your UserDefault if you want to keep previous sign-in status.

    In my case,

    public func resetUserDafault() {
    
       let userDefaults = UserDefaults.standard
    
       let dict = UserDefaults.standard.dictionaryRepresentation()
    
       for key in dict.keys {
    
           //GoogleSignIn take this key to check previous signin status
    
           if key == "GID_AppHasRunBefore"{
    
               continue
    
           }
    
           userDefaults.removeObject(forKey: key);
    
      }
    
      UserDefaults.standard.synchronize()
    
    }
    
    override func viewDidLoad() {
    
        super.viewDidLoad()
    
        //After doing it, my application is working properly now.
    
        if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{
    
            GIDSignIn.sharedInstance().signInSilently()
    
        }
        else{
    
            //not sign in
    
        }
    
    }
    
    0 讨论(0)
  • 2020-12-20 11:55

    Here are the error codes from GIDSignIn.h. The -4 code is sent by signInSilently when there are no auth tokens in the keychain. See comments.

    // A list of potential error codes returned from the Google Identity SDK.
    typedef NS_ENUM(NSInteger, GIDSignInErrorCode) {
      // Indicates an unknown error has occured.
      kGIDSignInErrorCodeUnknown = -1,
      // Indicates a problem reading or writing to the application keychain.
      kGIDSignInErrorCodeKeychain = -2,
      // Indicates no appropriate applications are installed on the user's device which can handle
      // sign-in. This code will only ever be returned if using webview and switching to browser have
      // both been disabled.
      kGIDSignInErrorCodeNoSignInHandlersInstalled = -3,
      // Indicates there are no auth tokens in the keychain. This error code will be returned by
      // signInSilently if the user has never signed in before with the given scopes, or if they have
      // since signed out.
      kGIDSignInErrorCodeHasNoAuthInKeychain = -4,
      // Indicates the user canceled the sign in request.
      kGIDSignInErrorCodeCanceled = -5,
    };
    

    For the Google SDKs in general I've found the header file comments are actually a pretty good place to look, usually more informative than any published documentation.

    0 讨论(0)
  • 2020-12-20 11:55

    I've had the same problem. The problem was in method:

    [[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.stream.read", @"https://www.googleapis.com/auth/plus.me"]];
    

    you should call it before:

    [[GIDSignIn sharedInstance] hasAuthInKeychain]    
    

    and

    [[GIDSignIn sharedInstance] signIn]   
    
    0 讨论(0)
  • 2020-12-20 12:02

    If you are using a custom button then you need to check auth in keychain.

    if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{
    
        GIDSignIn.sharedInstance().signInSilently()
    
    }
    else{
    
        GIDSignIn.sharedInstance().signIn()
    
    }
    
    0 讨论(0)
  • 2020-12-20 12:03

    Both Igor and Spydy worked for me

    A swift version of Igor's post

        GIDSignIn.sharedInstance().uiDelegate = self
    
        GIDSignIn.sharedInstance()?.hasAuthInKeychain()
        GIDSignIn.sharedInstance()?.signIn()
    
        // Uncomment to automatically sign in the user.
        GIDSignIn.sharedInstance().signInSilently()
    

    or from Spydy.

        // google sign in setup
        GIDSignIn.sharedInstance().uiDelegate = self
        if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{
            GIDSignIn.sharedInstance().signInSilently()
        }
        else{
            GIDSignIn.sharedInstance().signIn()
        }
    
    0 讨论(0)
提交回复
热议问题