首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >谷歌登录服务器验证码为空的iOS?

谷歌登录服务器验证码为空的iOS?
EN

Stack Overflow用户
提问于 2017-02-24 17:20:44
回答 6查看 3.4K关注 0票数 3

我们有谷歌在我们的application.We登录时提供的serverClientID登录请求。

我们将user.serverAuthCode设置为nil。

我们的请求如下:

代码语言:javascript
复制
func googleLogin(){
            var configureError: NSError?
            GGLContext.sharedInstance().configureWithError(&configureError)
            assert(configureError == nil, "Error configuring Google services: \(configureError)")

            let gid = GIDSignIn.sharedInstance()
            if let path = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist") {
                let myDict = NSDictionary(contentsOfFile: path)
                gid?.serverClientID = "our servers cliet id as configured over firebase"

// This client id of our ios app we are getting from
// GoogleService-info.plist 
                gid?.clientID = myDict!.value(forKey: "CLIENT_ID") as! String

            }
            //        gid?.serverClientID = "our_servers" // TODO: fetch from plist
            gid?.scopes.append("https://www.googleapis.com/auth/gmail.readonly")
            print("\nClient Id: \(gid!.clientID!) Server Client Id: \(gid!.serverClientID!)\n")
            gid?.delegate = self

        }

我们尝试获取serverAuthCode,如下所示:

代码语言:javascript
复制
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error == nil) {
            // Perform any operations on signed in user here.
            let auth = user.serverAuthCode
            print(auth)
            let fullName = user.profile.name
         } else {
            print("\(error.localizedDescription)")
        }

    }

但是它的serverAuthCode是空的,我们不确定可能出了什么问题。

EN

回答 6

Stack Overflow用户

发布于 2018-09-20 20:08:01

问题是,只有在用户第一次登录时才会发送服务器授权码。用户必须刚刚看到确认屏幕。在所有后续登录中,不会发送任何服务器授权码。转到https://security.google.com/settings/security/permissions并删除你的应用程序的凭据,然后你会得到一个服务器认证码。

票数 8
EN

Stack Overflow用户

发布于 2017-02-24 17:41:38

GIDSignInDelegate ,GIDSignInUIDelegate使用2个代理。

代码语言:javascript
复制
let signin : GIDSignIn = GIDSignIn .sharedInstance()
signin.shouldFetchBasicProfile = true;
signin.uiDelegate = self
signin.delegate = self
GIDSignIn.sharedInstance().signInSilently() // this for swift 3.0
GIDSignIn.sharedInstance().signIn()

谷歌会显示一个视图,提示用户使用登录

代码语言:javascript
复制
func sign(_ signIn: GIDSignIn!,
          present viewController: UIViewController!) {
    self.present(viewController, animated: true, completion: nil)
}

谷歌关闭“登录到”视图

代码语言:javascript
复制
func sign(_ signIn: GIDSignIn!,
          dismiss viewController: UIViewController!) {
    self.dismiss(animated: true, completion: nil)
}

已完成登录

代码语言:javascript
复制
    public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {

        if (error == nil) {
            // Perform any operations on signed in user here.
            let userId = user.userID                  // For client-side use only!
            let idToken = user.authentication.idToken // Safe to send to the server
            let fullName = user.profile.name
            let givenName = user.profile.givenName
            let familyName = user.profile.familyName
            let email = user.profile.email
            let imageurl = user.profile.imageURL(withDimension: 1080)
            print("Gmail id %@" ,userId!)
            print("Gmail token %@" ,idToken!)
            print("Gmail full name %@" ,fullName!)
            print("Gmail first name %@" ,givenName!)
            print("Gmail last name %@" ,familyName!)
            print("Gmail emailid %@" ,email!)

            print("Gmail id %@" ,imageurl!)

        } else {
            print("\(error.localizedDescription)")
        }
    }

别忘了在info.plist文件中添加这个

代码语言:javascript
复制
  <dict>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>com.googleusercontent.apps.************************</string>
        </array>
    </dict>
票数 4
EN

Stack Overflow用户

发布于 2021-01-20 18:22:02

使用登录谷歌Swift/ IOS

我可以用以下命令解决同样的问题:

googleUser.authentication.idToken

代码语言:javascript
复制
    func sign(_ signIn: GIDSignIn!, didSignInFor googleUser: GIDGoogleUser!, withError error: Error!) {
    if let error = error {
        if (error as NSError).code == GIDSignInErrorCode.hasNoAuthInKeychain.rawValue {
            print("The user has not signed in before or they have since signed out.")
        } else {
            print("\(error.localizedDescription)")
        }
        return
    }
    // Signed in successfully, forward credentials to MongoDB Realm.
    
    let app = App(id: "application-0-fkaqn")
    let id  = googleUser.authentication.idToken
    let credentials = Credentials.googleId(token: googleUser.authentication.idToken)
    
    app.login(credentials: credentials) { result in
        DispatchQueue.main.async {
            switch result {
            case .failure(let error):
                print("Failed to log in to MongoDB Realm: \(error)")
            case .success(let user):
                
                print("Successfully logged in to MongoDB Realm using Google OAuth.")
                
                
                // Now logged in, do something with user
                // Remember to dispatch to main if you are doing anything on the UI thread
            }
        }
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42434887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档