无法从Facebook Graph访问电子邮件

时间:2018-01-24 08:59:18

标签: ios swift facebook facebook-graph-api

我一直在使用以下代码从Facebook图表中获取一些细节,

    func getFBUserData(){
    let parameters =  ["fields": "id, first_name, picture.type(large), email, last_name, gender"]
    let graphRequest = FBSDKGraphRequest(graphPath: "/me", parameters: parameters)
    graphRequest?.start { (connection, result, error) in
        if let error = error {
            print(error.localizedDescription)
            print("Data Not Received")
        } else {
            let userID = FBSDKAccessToken.current().userID
            let URL = "http://graph.facebook.com/\(userID)/picture?type=large"
            print(URL)

            let json = JSON(result)
            print("FB Json \(json)")
            let fid = json["id"].stringValue
            let email = json["email"].stringValue
            let name =  json["first_name"].stringValue
            let lName = json["last_name"].stringValue
            let sex = json["gender"].stringValue
            var imgURLString = "http://graph.facebook.com/" + fid + "/picture?type=large"
            print(imgURLString)


            self.UploadSocialAccount(name: name,lname: lName, email: email, picture: imgURLString, gender: sex)
            let manager = FBSDKLoginManager()
            manager.logOut()

        }
    }
}

出于某种原因,除了电子邮件之外,我还能获得所有其他详细信息。 在函数中需要更改什么才能获得所有参数?

2 个答案:

答案 0 :(得分:0)

可以访问的电子邮件,如果它当前处于活动状态并具有值,可用于登录yahoo或gmail或其支持者,但如果它已过期,您将获得nil尽管数据有效登录Facebook

        let facebookLogin = FBSDKLoginManager()

         facebookLogin.logOut() 

        facebookLogin.logIn(withReadPermissions: ["public_profile","email", "user_friends"], from:self, handler:
        {  
            (facebookResult, facebookError) -> Void in

            let requestMe:FBSDKGraphRequest = FBSDKGraphRequest.init(graphPath: "/me", parameters: ["fields": "id,email, first_name, last_name, gender, picture"])

            let connection : FBSDKGraphRequestConnection = FBSDKGraphRequestConnection()



            connection.add(requestMe, completionHandler: { (con, results, error) in

                //access_token

                if ((error) == nil)

                {
                     let dic = results as! NSDictionary 

                    DataClass.shared.facebookId = dic.value(forKey: "id") as! String

                    print("FBis111123 \(DataClass.shared.facebookId)")

                    let fname = dic.value(forKey: "first_name")

                    let lname = dic.value(forKey: "last_name")

                    let email = (dic.value(forKey: "email"))

                    let picture = dic.value(forKey: "picture") as! NSDictionary

                    let data = picture["data"]! as! NSDictionary

                }
                else

                {


                }

            })


            connection.start()

            print("token:\(String(describing: facebookResult?.token))")


            if facebookError != nil
            {
                print("Facebook login failed. Error \(String(describing: facebookError))")
            }
            else if (facebookResult?.isCancelled)!
            { 
                print("Facebook login was cancelled.")

            }
            else
            {
                 print("Success.")

            }
    })

答案 1 :(得分:0)

您是否已请求阅读电子邮件地址的许可?否则,您只能阅读facebook的公开个人资料。要访问其他信息,您必须特别要求用户许可。查看本网站:

https://developers.facebook.com/docs/facebook-login/ios/permissions

“在发送Graph API请求之前,您应该检查必要的权限并在需要时请求它们。”

您希望在登录按钮中执行此操作,例如:

func presentFBButton() {
    let loginButton = FBSDKLoginButton()
    loginButton.delegate = self
    // Permissions below this comment
    loginButton.readPermissions = ["email", "public_profile"]

    view.addSubview(loginButton)
    loginButton.frame = CGRect(x: 50, y: self.view.center.y + 123, width: view.frame.width - 100, height: 38)
}