【问题标题】:Cannot Access Email from Facebook Graph无法从 Facebook Graph 访问电子邮件
【发布时间】:2018-07-03 06:12:35
【问题描述】:

我一直在使用以下代码从 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()

        }
    }
}

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

【问题讨论】:

  • 用户是否授予您的应用电子邮件权限?
  • 我认为没有.. 如何授予应用电子邮件权限?在这个函数中..?
  • 您在用户登录时请求用户许可
  • 那么,我应该在 info.plist 中设置它吗?

标签: ios swift facebook facebook-graph-api


【解决方案1】:

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

        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.")

            }
    })

【讨论】:

  • 您仍然可以使用它的电子邮件地址登录,我的意思是如果它是雅虎电子邮件,您可以在雅虎应用程序中打开它或
【解决方案2】:

您是否申请了阅读电子邮件地址的权限?否则,您只能阅读 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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多