5

下午好!我需要从 Facebook 获取用户数据。我成功获取了用户名、id、照片等数据。但是当您尝试查询婚姻状况等时,我会得到一个空字典的响应。我试图提出不同的要求,但总是得到这样的结果。我还阅读了这些主题官方 Facebook 网站这个这个,我该如何解决这个问题?

我的示例代码

   static func getUserRelationship() {
    if (FBSDKAccessToken.currentAccessToken() != nil) {
        guard let currentUser = getCurrentFacebookUser() else { return }
        FBSDKGraphRequest(graphPath: "/\(currentUser.id)/family", parameters: nil, HTTPMethod: "GET").startWithCompletionHandler({ (requestConnection, result, error) in
            if error == nil {
                let dictionary = result as! [String : AnyObject]
                let array = dictionary["data"]
                print("facebook", result, dictionary, array?.count)
            } else {
                print(error.localizedDescription)
            }
        })
    } else {
        getDataLogin({
            getUserBirthday()
            }, fails: { (error) in

        })
    }
}

我在控制台中看到了结果

    facebook {
    data =     (
    );
}
4

2 回答 2

13

您的 GraphRequest 不正确。如果要获取用户数据,graphPathe 应该是“我”,并且您应该请求参数以获取关系状态和其他信息。而且您还需要在 LogInWithReadPermissons 中请求公开个人资料,因此在读取许可中:-

    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web
    fbLoginManager.logInWithReadPermissions(["public_profile","email"], fromViewController: self) { (result, error) -> Void in
        if error != nil {
            print(error.localizedDescription)
            self.dismissViewControllerAnimated(true, completion: nil)
        } else if result.isCancelled {
            print("Cancelled")
            self.dismissViewControllerAnimated(true, completion: nil)
        } else {

        }
    }

并且在检索信息时:-

   FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, relationship_status"]).startWithCompletionHandler({ (connection, result, error) -> Void in
            if (error == nil){
                let fbDetails = result as! NSDictionary
                print(fbDetails)
            }
        })

顺便说一句,如果你想要婚姻状况,你应该使用 graphPath 作为“我”而不是“/{user-id}/family”。您可以使用它来获取该人的家庭关系。

于 2016-06-02T10:52:52.227 回答
2

使用 swift 3 获取 facebook 用户信息更新

  FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).start(completionHandler: { (connection, result, error) -> Void in
        if (error == nil){
            let fbDetails = result as! NSDictionary
            print(fbDetails)
        }else{
            print(error?.localizedDescription ?? "Not found")
        }
    })
于 2017-03-07T12:18:06.413 回答