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

我有这种情况下,GIDSignIn.sharedInstance()。signInSilently()返回一个错误:

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

我似乎无法find任何有关此问题的谷歌login文档或stackoverflow的文档。

我期待这个错误发生,如果我请求一个沉默login没有以前signedIn用户。 但令我惊讶的是,甚至当我有一个用户以前签署,我试图在几秒钟后第二次无声login。

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

GIDSignIn.sharedInstance().currentUser 

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

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

谢谢

这里是来自GIDSignIn.h的错误代码。 当密钥链中没有authentication令牌时,通过signInSilently发送-4代码。 看评论。

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

对于一般的Google SDK,我发现头文件注释实际上是一个相当不错的地方,通常比任何已发布的文档都更具信息性。

我有同样的问题。 问题出在方法上:

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

你应该在之前调用它:

 [[GIDSignIn sharedInstance] hasAuthInKeychain] 

 [[GIDSignIn sharedInstance] signIn] 

我在这里有同样的问题,但我终于find了答案。 我发现GoogleSignIn在保持以前的login状态时采取了UserDefault。 请检查您是否使用UserDefault开发您的应用程序。 如果你这样做,请确保你不会删除你的UserDefault中的所有数据,如果你想保持以前的login状态。

就我而言,

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

伊戈尔·罗塔鲁有正确的答案。 关键是在使用signInSilently之前设置范围。 它将检查用户是否曾经使用之前设置并login过的作用域login过。

请参阅保存当前GIDGoogleUser的答案, 而不是在每次启动时都login

您应该遵守GIDSignInUIDelegate协议而不实施这些方法。

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

它会解决你的错误-4。