我试图获取 IOS 应用程序的 facebook 好友列表,但收到来自 facebook 的空响应。
如何快速获取面子书好友列表?
在 Graph API v2.0 或更高版本中,调用 /me/friends 返回安装相同应用程序的人的朋友。此外,您必须向每个用户请求 user_friends 权限。user_friends 不再默认包含在每次登录中。每个用户必须授予 user_friends 权限才能出现在对 /me/friends 的响应中。
要获取正在使用您的应用程序的朋友列表,请使用以下代码:
let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
let request = FBSDKGraphRequest(graphPath: "me/friends", parameters: params)
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error != nil {
let errorMessage = error.localizedDescription
/* Handle error */
}
else if result.isKindOfClass(NSDictionary){
/* handle response */
}
}
如果您想访问不使用应用程序的朋友列表,请使用以下代码:
let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
let request = FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params)
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error != nil {
let errorMessage = error.localizedDescription
}
else if result.isKindOfClass(NSDictionary){
/* Handle response */
}
}
要获取好友列表,必须在登录时允许用户权限user_friends。对于swift 3.0添加以下代码以获取好友列表:
let params = ["fields": "id, first_name, last_name, middle_name, name, email, picture"]
FBSDKGraphRequest(graphPath: "me/taggable_friends", parameters: params).start { (connection, result , error) -> Void in
if error != nil {
print(error!)
}
else {
print(result!)
//Do further work with response
}
}
我希望它有效!
请记住,只有那些正在使用您的应用程序的朋友会被获取,并且您应该在登录时获取用户对user_friends的读取权限。
var fbRequest = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
fbRequest.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
} else {
println("Error Getting Friends \(error)");
}
}
参考:https ://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read
只是在这里建立一个工会来解决我的问题:
来自 Dheeraj Singh,让朋友使用您的应用程序:
var request = FBSDKGraphRequest(graphPath:"/me/friends", parameters: nil);
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
} else {
println("Error Getting Friends \(error)");
}
}
并让所有 Facebook 朋友,从 Meghs Dhameliya,移植到 swift:
var request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: nil);
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
println("Friends are : \(result)")
} else {
println("Error Getting Friends \(error)");
}
}
您可以使用 taggable_friends Graph API Reference for User Taggable Friend获取好友列表
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/taggable_friends"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
}];
Swift - 4
这只会返回您使用同一应用程序的 Facebook 朋友。
// First you have to check that `user_friends` has associated with your `Access Token`, If you are getting false, please allow allow this field while you login through facebook.
if FBSDKAccessToken.current().hasGranted("user_friends") {
// Prepare your request
let request = FBSDKGraphRequest.init(graphPath: "me/friends", parameters: params, httpMethod: "GET")
// Make a call using request
let _ = request?.start(completionHandler: { (connection, result, error) in
print("Friends Result: \(String(describing: result))")
})
}
通过user_friends权限,您可以访问也使用您的应用的朋友列表,因此如果您的任何朋友都没有使用您的应用,则该列表将为空。查看user_friends 权限参考