iOS Facebook SDK:FBSDKGraphRequest不返回电子邮件(Swift)

问题描述 投票:2回答:3

我在这里看到过类似的问题,但似乎没有解决我的问题。

我目前正在创建一个iOS应用程序,它使用Facebook SDK登录并返回用户信息。

我已成功登录Facebook并收到所需信息。但是,尽管允许电子邮件的读取权限,但请求仍未返回电子邮件。

    /********************
    Place Facebook login button
    *********************/
    let loginButton = FBSDKLoginButton()
    view.addSubview(loginButton)
    loginButton.delegate = self
    loginButton.readPermissions = ["email", "public_profile"]
    //add button constraints
    loginButton.center = view.center



}

/**
* Functions called when user handed back from facebook login
**/
func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("Did log out of facebook")
}
/**
 * Functions called when user handed back from facebook login
 **/
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
    } else
    {
        print("Successfully logged in with facebook")
        FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start
            { (connection, result, err) in
            if err != nil{
                print("Failed to start graph request: ", err)
                return
            }
            print(result)
        }
    }
}
ios swift facebook facebook-sdk-4.0
3个回答
0
投票

根据Facebook Graph API tool,您无法从FBSDKGraphRequest获取用户电子邮件。 Facebook仅返回用于登录的令牌。如果您使用Facebook登录Firebase,则可以使用CurrentUser.currentEmail方法获取电子邮件。

 let login = FBSDKLoginManager()

    login.logIn(withReadPermissions: ["email","user_posts","public_profile"], from: self, handler: {
        (result: FBSDKLoginManagerLoginResult?, error: Error?) in
        if error != nil {
            print("Process error")
            return
        }
        else if (result?.isCancelled)! {
            print("Cancelled")
        }
        else {
            //Success!
            let credential = FIRFacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
            self.firebaseLogin(credential)
        }
    })

0
投票
 import FBSDKCoreKit
        import FBSDKLoginKit
        import FBSDKShareKit


      @IBAction func btnFbPressed(_ sender: Any) {

            let loginManager = FBSDKLoginManager()

            loginManager.logIn(withReadPermissions: ["public_profile", "email"], from: self) { (loginResult, error) in

                if error != nil {
                    print(error!)

                }
                else {

                    if (loginResult?.grantedPermissions.contains("email"))! {
                        self.getFBUserData()
                    }

                }
            }


        }




        func getFBUserData(){
            if((FBSDKAccessToken.current()) != nil){

                UserDefaults.standard.set(FBSDKAccessToken.current().tokenString, forKey: "fbToken")

                FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
                    if (error == nil) {

                        print(result as Any)

                        let dictionary = result as! NSDictionary
                        print(dictionary)

                        let hud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        hud.tintColor = .black
                        hud.mode = .indeterminate
                        hud.label.text = "Loading"
                        hud.animationType = .fade
                        hud.show(animated: true)

                        let postString = String(format: "login_type=facebook&email=%@&id=%@&device=0", arguments: [dictionary.value(forKey: "email") as! String, dictionary.value(forKey: "id") as! String])
     }
                })
            }
        }

0
投票

好吧,我思考了很长一段时间,然后来到this link,这表明如果你没有确认的电子邮件地址,我将不会在我的代码的登录部分找回它。

let request = GraphRequest(graphPath: "me", parameters: ["fields": "id, email, name, first_name, last_name, picture.type(large)"], accessToken: AccessToken.current, httpMethod: .GET, apiVersion: "2.12")
                request.start { (response, result) in
                    switch result {
                    case .success(let value):
                        print(value.dictionaryValue!)
                        self.responseJSON = JSON(value.dictionaryValue!)
                        let fullName = self.responseJSON["first_name"].stringValue
                        let firstName = self.responseJSON["first_name"].stringValue
                        let lastName = self.responseJSON["last_name"].stringValue
                        let email = self.responseJSON["email"].stringValue
                        let idFb = self.responseJSON["id"].stringValue
                        let picture = self.responseJSON["picture"]["data"]["url"].stringValue

                        print("user id: \(idFb), firstName: \(firstName), fullname: \(fullName), lastname: \(lastName), picture: \(picture), email: \(email)")

                    print("ALL FIELDS PUBLISHED")


                    if let delegate = self.responseDelegate {
                        delegate.responseUpdated(self.responseJSON, forLogin: "FB")
                    }

                case .failed(let error):
                    print(error)
                }
            }
        }

因此,当我到达并确认我的电子邮件地址时,它给了我回复的电子邮件。以下是一些附加快照。

enter image description here

enter image description here

enter image description here

这就是我如何做到的。后来我实现了,如果没有返回电子邮件,我会显示一条警告,警告用户没有与该Facebook帐户关联的电子邮件,他们确认他们的电子邮件或继续在TextField警报中提供单独的电子邮件地址。那就是那个。

© www.soinside.com 2019 - 2024. All rights reserved.