首页 > 解决方案 > 在 iOS 中谷歌登录后无法获取 Authcode (swift)

问题描述

问题: 在 iOS 应用程序中,我使用 google signIn 来促进获取 authCode 的过程,稍后将其发送到服务器并在 google 日历 API 中交换 refresh_token。坏消息是它user.serverAuthCode总是为零。

现在发生了什么: 我已经按照谷歌的文档来实现它。此时,当我单击 google 登录按钮时,我会转到一个页面,在该页面中填写我的凭据并同意所有范围。然后它将我重定向回 iOS 应用程序,但我没有得到 authCode。

代码:

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate{
    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {
            print("Authcode is : \(user.serverAuthCode)") // This is nil :(
        }
    }


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

        GIDSignIn.sharedInstance().clientID = "MY_CLIENT_ID"
        GIDSignIn.sharedInstance().delegate = self
        GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/calendar")     
        GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/calendar.events")

        return true
    }

 }

我还在目标信息中添加了 URL 类型,如谷歌文档中所述

最后,我有一个 viewController 用于GIDSignInButton启动登录过程。


有什么我想念的吗?我看了一下这篇文章,但我无法解决问题。感谢您的时间!

标签: iosswiftgidsignin

解决方案


经过一段时间的搜索,找到了解决方案:)

添加后解决的问题:

GIDSignIn.sharedInstance()?.serverClientID = "SERVER_CLIENT"

如果您不知道如何获取这些:SERVER_CLIENT vs CLIENT_ID


推荐阅读