下午好!我需要从Facebook获取用户数据。我成功获取了用户名,ID,照片等数据。但是当你尝试查询婚姻状况时,我得到一个空字典的回复。我尝试提出不同的请求,但总是得到这样的结果。我还阅读了这些主题official Facebook site,this,this,如何解决此问题?
我的示例代码
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 = (
);
}
答案 0 :(得分:14)
您的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”。你可以用它来获得这个人的家庭关系。
答案 1 :(得分:2)
使用swift 3获取Facebook用户信息更新
<html>
<title> HTML Quiz </title>
<meta charset="UTF-8">
<script>
var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;
var questions = [
["What does HTML stand for?", "Hypertext Markup Language", "Hypertrain Master Language", "Hypertext Marking Language", "A"],
["What was Java's first project name?", "Mocha", "Perseus", "Green", "C"],
["This programming language was the predecessor of Java, developed by James Gosling", "C++", "Oak", "Python", "B"],
["What does CSS stand for?", "Cascading Style Sheet", "Calculating Solutions Sheet", "Computer String Sheet", "A"],
["What was the dominant programming language before Java was created?", "C#", "C++", "Python", "B"],
["Can CSS be used in tandem with HTML and Java?", "True", "False", "Neither", "A"],
["What is the main foundation of all programming language?", "Binary/Machine Language", "Logic", "Code", "A"]
];
function _(x){
return document.getElementById(x);
}
function renderQuestion() {
test = _("test");
if (pos >= questions.length) {
test.innerHTML = "<h2> You got "+correct+" of "+questions.length+" questions correct </h2>";
_("test_status").innerHTML = "Test Completed!";
pos = 0;
correct = 0;
return false;
}
_("test_status").innerHTML = "Question "+(pos+1)+" of "+questions.length;
question = questions[pos][0];
chA = questions[pos][1];
chB = questions[pos][2];
chC = questions[pos][3];
test.innerHTML = "<h3>"+question+"</h3>";
test.innerHTML += "<input type='radio' name='choices' required value='A'>"+chA+"<br>";
test.innerHTML += "<input type='radio' name='choices' required value='B'>"+chB+"<br>";
test.innerHTML += "<input type='radio' name='choices' required value='C'>"+chC+"<br><br>";
test.innerHTML += "<button onclick='checkAnswer()'>Submit Answer</button>";
}
function checkAnswer(){
choices = document.getElementsByName("choices");
for(var i=0; i<choices.length; i++) {
if (choices[i].checked) {
choice = choices[i].value;
}
}
if (choice == questions[pos][4]){
correct++;
}
pos++;
renderQuestion();
}
window.addEventListener("load", renderQuestion, false);
</script>
<style>
div#test{ border:#000 1px solid; padding: 10px 40px 40px 40px; }
</style>
<body>
<center>
<h2 id="test_status"> </h2>
<div id="test"></div>