swift - 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!
}
}
解决方案
如果有人遇到同样的问题,请在 Xcode 的 URL 类型中添加iOS URL 方案,并且不要像许多教程中所说的那样使用反向 url。使用 Google 提供的 CLIEND ID:
let signInConfig = GIDConfiguration.init(clientID: "CLIENT ID ")
推荐阅读
- java - 如何在 java 中编写这个问题,或者我如何将一个客户端和 4 个服务器相互连接
- javascript - 如何使用 Joi 验证对象键的值是否应该出现在另一个对象中?
- reactjs - Material UI - 覆盖 InputBase 的禁用样式
- linux - 子域重定向到 haproxy 中的同一后端
- c - RoundRobin 使用链表。完成时间计算问题
- pyqt5 - 如何在 PyQt5 中为 dialog.exec 生成接受信号
- node.js - 跳过可选依赖:fsevents@1.2.9 不支持的平台:想要 {"os":"darwin","arch":"any"}(当前:{"os":"win32","arch":"x64" })
- javascript - JavaScript React async/await 返回错误的正文?
- c - 包含函数指针数组的结构需要多少内存?
- python - 在特定文本之后和特定文本之前刮掉文本