首页 > 解决方案 > 400 Invalid_request 您无法登录此应用,因为它不符合 Google 的 OAuth 2.0 保护应用安全政策

问题描述

当我尝试在我的 IOS 应用程序中向 Google 注册时出现此错误。我得到了 REVERSED-CLIEND_ID,它看起来像这样:com.googleusercontent.apps ..... 到目前为止,我可以打开 google 窗口并收到 400 错误。

应用委托:

class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        ApplicationDelegate.shared.application(application,didFinishLaunchingWithOptions: launchOptions)
        GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in
            if error != nil || user == nil {
                // Show the app's signed-out state.
            } else {
                // Show the app's signed-in state.
            }
        }
        return true
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        ApplicationDelegate.shared.application(
            app,
            open: url,
            sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
            annotation: options[UIApplication.OpenURLOptionsKey.annotation]
        )
        var handled: Bool
        
        handled = GIDSignIn.sharedInstance.handle(url)
        if handled {
            return true
        }
        
        // Handle other custom URL types.
        
        // If not handled by this app, return false.
        return false
    }
}

视图控制器:

    let signInConfig = GIDConfiguration.init(clientID: "REVERSED_URL_THING")

  @IBAction func googleRegister(_ sender: UIButton) {
        GIDSignIn.sharedInstance.signIn(
            with: signInConfig,
            presenting: self
        ) { user, error in
            guard error == nil else { return }
            guard let user = user else { return }

            // Your user is signed in!
        }
    }
    

标签: swiftxcodeswift5facebook-ios-sdkgoogle-developer-tools

解决方案


如果有人遇到同样的问题,请在 Xcode 的 URL 类型中添加iOS URL 方案,并且不要像许多教程中所说的那样使用反向 url。使用 Google 提供的 CLIEND ID:

let signInConfig = GIDConfiguration.init(clientID: "CLIENT ID ")

推荐阅读