Swift Firebase Facebook登录 - 获取姓名,但电子邮件返回nil

时间:2018-01-10 14:19:22

标签: swift facebook firebase facebook-graph-api firebase-authentication

最近我的facebook登录还没有运行。它不保存到我的数据库中,但在Firebase身份验证中,它显示有人使用Facebook登录,但电子邮件是空白的。它不会将任何数据保存到我的数据库中。电子邮件返回零。我的谷歌登录工作。我检查了所有的连接,他们似乎都很好。 (我本可以错过一些东西)任何人对我应该检查的连接有任何建议吗?不知道该怎么做......

更多信息

我打印了我的图形请求...不确定这是否有助于调试

let loginManager: FBSDKLoginManager = FBSDKLoginManager()
        loginManager.logIn(withReadPermissions: self.facebookPermissions, from: self, handler: { (result, error) in
            if error != nil {
                loginManager.logOut()
                let message: String = "An error has occured. \(String(describing: error))"
                let alertView = UIAlertController(title: "Alert", message: message, preferredStyle: UIAlertControllerStyle.alert)
                alertView.addAction(UIAlertAction(title: "Ok ", style: UIAlertActionStyle.default, handler: nil))
                self.present(alertView, animated: true, completion: nil)
            } else if (result?.isCancelled)! {
                // user cancelled login
                loginManager.logOut()
            } else {
                let accessToken = FBSDKAccessToken.current()
                guard let accessTokenString = accessToken?.tokenString else { return }
                let credential = FacebookAuthProvider.credential(withAccessToken: accessTokenString)

                Auth.auth().signIn(with: credential) { (user, error) in
                    if (error != nil) {
                        // handle error
                        print(error ?? "Error")
                    } else {
                        let ref = Database.database().reference()

                        // guard for user id
                        guard let uid = user?.uid else {
                            return
                        }
                        let usersReference = ref.child("user_profiles").child(uid)

                        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, email"])
                        graphRequest.start(completionHandler: { (connection, result, error) -> Void in
                            if error != nil {
                                // Process error
                                print("Error: \(String(describing: error))")
                            } else {
                                guard let data: [String:AnyObject] = result  as? [String:AnyObject] else {
                                    print("Can't pull data from JSON")
                                    return
                                }
                                guard let userName: String = data["name"] as? String else {
                                    print("Can't pull username from JSON")
                                    return
                                }
                                guard let userID: String = data["id"] as? String else {
                                    print("Can't pull ID from JSON")
                                    return
                                }
                                let imgURLString = "http://graph.facebook.com/\(userID)/picture?type=large" as String
                                guard let userEmail: String = data["email"] as? String else {
                                    print("Can't pull email from JSON")
                                    print("Error: \(String(describing: error))")
                                    return
                                }


                                // initial # posts = 0
                                let values = ["name": userName, "email": userEmail, "facebookID": userID, "profPicString": imgURLString] as [String : Any]

                                // update database with new user
                                usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
                                    // error in database save
                                    if err != nil {
                                        print(err ?? "Error saving user to database")
                                        return
                                    }
                                })
                            }
                        })
                        self.dismiss(animated: false, completion: nil)
                        // Present the main view
                        if let viewController = self.storyboard?.instantiateViewController(withIdentifier: "Customer Profile") {
                            UIApplication.shared.keyWindow?.rootViewController = viewController
                            self.dismiss(animated: true, completion: nil)
                        }
                    }
                }
            }
        })

enter image description here

1 个答案:

答案 0 :(得分:2)

使用 Swift 5 ,我在 providerData 中找到了该电子邮件,该电子邮件由FIRUserInfo组成:

if AccessToken.current != nil {
    let credential = FacebookAuthProvider.credential(withAccessToken: AccessToken.current!.tokenString)
    Auth.auth().signIn(with: credential) { (res, err) in
        if err != nil || res == nil {
            //...
            return
        }
        guard let providerData = res?.user.providerData else {
            //...
            return
        }
        for firUserInfo in providerData {
            print(firUserInfo.providerID)
            print(firUserInfo.email ?? "Email not found")
        }
    }
}