signInSilently()生成错误代码= -4

问题描述 投票:19回答:8

我遇到这种情况,GIDSignIn.sharedInstance()。signInSilently()返回错误:

错误域= com.google.GIDSignIn代码= -4“无法完成操作。(com.google.GIDSignIn错误-4。)”

我似乎无法在google signin docs或stackOverflow下找到任何关于此错误的文档。

如果我为之前没有签名的用户请求静默signIn,我希望这会发生错误。但令我惊讶的是,当我有一个用户以前签名并且我尝试在几秒钟后第二次静默登录时,甚至会发生这种情况。

我遇到困难的第二个问题是确定是否有用户登录使用:

GIDSignIn.sharedInstance().currentUser

这是一个零或一个GIDGoogleUser对象。

任何帮助在这个问题上取得进展将不胜感激。

谢谢

ios swift google-signin
8个回答
10
投票

以下是GIDSignIn.h的错误代码。 -4代码由signInSilently在钥匙串中没有auth令牌时发送。看评论。

// 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,
};

对于谷歌SDK,我发现头文件注释实际上是一个非常好看的地方,通常比任何已发布的文档更具信息性。


9
投票

我遇到了同样的问题。问题出在方法上:

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

你应该先叫它:

[[GIDSignIn sharedInstance] hasAuthInKeychain]    

[[GIDSignIn sharedInstance] signIn]   

4
投票

我在这里遇到了同样的问题,但我终于找到了答案。我发现GoogleSignIn采用UserDefault来保持之前的登录状态。请检查您是否使用UserDefault来开发您的应用程序。如果您这样做,请确保如果您想保留以前的登录状态,则不会删除UserDefault中的所有数据。

就我而言,

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

    }

}

2
投票

伊戈尔罗塔鲁有正确的答案。关键是在使用signInSilently之前设置范围。它将检查用户之前是否曾使用您之前设置和登录的范围登录。


2
投票

如果您使用自定义按钮,则需要检查钥匙串中的身份验证。

if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{

    GIDSignIn.sharedInstance().signInSilently()

}
else{

    GIDSignIn.sharedInstance().signIn()

}

0
投票

请参考Saving the current GIDGoogleUser instead of signing in on every launch的回答

你应该遵守GIDSignInUIDelegate协议而不实现这些方法。

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

它会修复你的错误-4。


0
投票

检查是否有互联网,然后与您的员工一起。当iPad未在辅助登录检查中连接时,我收到此错误。


0
投票

Igor和Spydy都为我工作

Igor的帖子的快速版本

    GIDSignIn.sharedInstance().uiDelegate = self

    GIDSignIn.sharedInstance()?.hasAuthInKeychain()
    GIDSignIn.sharedInstance()?.signIn()

    // Uncomment to automatically sign in the user.
    GIDSignIn.sharedInstance().signInSilently()

或来自Spydy。

    // google sign in setup
    GIDSignIn.sharedInstance().uiDelegate = self
    if GIDSignIn.sharedInstance().hasAuthInKeychain() == true{
        GIDSignIn.sharedInstance().signInSilently()
    }
    else{
        GIDSignIn.sharedInstance().signIn()
    }
© www.soinside.com 2019 - 2024. All rights reserved.