【问题标题】:iOS swift You must specify clientID Exception in google integrationiOS swift 您必须在 google 集成中指定 clientID 异常
【发布时间】:2016-12-03 03:42:53
【问题描述】:

代码:

let signIn = GPPSignIn.sharedInstance()
        signIn.shouldFetchGooglePlusUser = true
        signIn.clientID = "912597493260-qg351fl8olmnmjl8qobos8n6u909jp0o.apps.googleusercontent.com"
        signIn.scopes = [kGTLAuthScopePlusLogin];
        signIn.trySilentAuthentication();
        GIDSignIn.sharedInstance().signInSilently()
        signIn.delegate = self

由于未捕获的异常 'NSInvalidArgumentException',原因:'您必须指定 |clientID|对于 |GIDSignIn|

我仔细检查了我的代码。即使我设置了客户端 ID 也得到了这个异常。我哪里出错了?任何帮助将不胜感激。提前致谢

【问题讨论】:

  • 你有没有在你的Xcode项目中集成google配置文件。
  • 你找到解决办法了吗?

标签: ios swift


【解决方案1】:

我按照 Google 自己的指南添加登录 here。我一步一步地跟着它 - 也集成了谷歌配置文件。根据指南,如果包含配置文件,则不需要手动设置客户端 ID。不幸的是,当我运行应用程序并点击登录按钮时,我遇到了完全相同的错误:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“您必须指定 |clientID|对于 |GIDSignIn|'

解决方案:

由于某种原因,clientID 没有自动从配置文件中选择。我们应该直接配置 GIDSignIn 对象,(使用在 GoogleService-Info.plist 文件中找到的客户端 ID)在应用程序委托的 application:didFinishLaunchingWithOptions: 方法中:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
      // Initialize sign-in
      var configureError: NSError?
      GGLContext.sharedInstance().configureWithError(&configureError)
      assert(configureError == nil, "Error configuring Google services: \(configureError)")
      GIDSignIn.sharedInstance().clientID = "Cliend id From GoogleService-Info.plist file"
      GIDSignIn.sharedInstance().delegate = self
      return true
}

另外,如果您使用 Firebase,您也可以这样做:

GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID

【讨论】:

  • 这对我不起作用,但这个概念是正确的。我仍然收到相同的错误消息,所以我尝试了一些断点调试,发现在调用 didFinishLaunchingWithOptions 之前在我的 ViewController.swift 中调用了 GIDSignIn。我不确定为什么。添加 clientID 作为在 ViewDidLoad 中配置 Google 登录的一部分解决了该问题。
【解决方案2】:

默认情况下,自动生成的配置文件 GoogleService-Info.plist 将包含错误的凭据;它包括 Web 客户端凭据而不是 iOS 应用程序凭据。

您需要更正 GoogleService-Info.plist 中的 Client ID 和 Reverse Client ID。

由于这些凭据也用于您应用的 URLSchemes,因此您也需要在此处进行更正。

【讨论】:

    【解决方案3】:

    我也面临同样的问题。我按照https://firebase.google.com/docs/auth/ios/google-signin#swift_9 的文档执行了每一步。

    最后,我尝试在 Controller 的 viewDidLoad 上手动添加客户端 ID,经过长时间的努力,它成功了。

    参考下面的代码。用 GoogleService-info.plist 替换您的项目特定 Client-ID 代替 ClientID


    class IntroController: UIViewController {  
        override func viewDidLoad() {
            super.viewDidLoad()      
            GIDSignIn.sharedInstance().clientID = "*ClientID*"
            GIDSignIn.sharedInstance()?.presentingViewController = self
            GIDSignIn.sharedInstance().signIn()       
        }   
    }
    

    【讨论】:

      【解决方案4】:

      clientId 确实是从 .plist 文件中获取的。如果看起来不是,则您的代码很可能在正确配置之前尝试使用登录对象。在您的 configureWithError 行上设置一个断点,并确保在尝试设置委托、执行静默登录等之前该断点被命中。

      【讨论】:

        【解决方案5】:

        看起来登录方法现在已经被 google 更新了,我正在实施适用于 iOS 应用的 Google 日历,我找到了以下登录代码:

        func applicationDidFinishLaunching(_ application: UIApplication) {
                // Initialize sign-in
                var configureError: NSError?
                GGLContext.sharedInstance().configureWithError(&configureError)
                assert(configureError == nil, "Error configuring Google services: \(configureError!)")
            }
        

        在他们的文档中给出了同样的错误:

        Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'You must specify |clientID| for |GIDSignIn|'
        

        我拿了里面的台词:

        func applicationDidFinishLaunching(_ application: UIApplication)
        

        并将它们放入此方法并登录工作:

         func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
        

        参考代码:

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
        
            // Initialize sign-in
            var configureError: NSError?
            GGLContext.sharedInstance().configureWithError(&configureError)
            assert(configureError == nil, "Error configuring Google services: \(configureError!)")
            return true
        }
        

        【讨论】:

          【解决方案6】:

          您可能需要从https://console.firebase.google.com 而不是https://console.developers.google.com/ 获取GoogleService-Info.plist

          【讨论】:

            【解决方案7】:

            使用 Firebase 时还请记住,您必须在设置 clientId 之前调用 Firebase.configure() 函数。否则,它将不起作用。

            【讨论】:

              猜你喜欢
              • 2021-12-21
              • 2017-02-05
              • 2019-06-23
              • 1970-01-01
              • 1970-01-01
              • 2021-03-16
              • 2022-09-29
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多