Facebook Graph API - Getting mutual likes - ios

I have an issue when getting mutual likes with Facebook Graph API. The context is the following:
userA is the requester from an iOS application
userA and userB have granted "user_likes" permission both from the iOS app
userA and userB are not Facebook friends
My app Swift code for this part is the following:
self.FBLoginButton.readPermissions = ["public_profile", "email", "user_friends", "user_birthday", "user_about_me", "user_likes"]
...
...
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: userBFBid, parameters: ["fields": "context.fields(mutual_likes).limit(10)"])
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil) {
print("Error: \(error)")
} else {
print("res: \(result)")
}
})
The result is the following:
res: {
context = {
id = dXNlcl9jb250ZAXh0OgGQg20zLjrZAswOnuX6*******;
"mutual_likes" = {
data = (
);
summary = {
"total_count" = 0;
};
};
};
id = 101532998**********;
}
As you can see, the mutual likes are not returned.
I made the same request with userA and userC which are Facebook friends, and that works correctly, all their mutual likes are returned.
Again: userA, userB and userC have the app installed and granted the user_likes permission.
Any idea to fix it and to get mutual_likes with two users which are not Facebook friends?

I finally found the issue.
First of all: user likes must be visible by public in FB user account privacy configuration to be accessible by the API even if the user_likes permission is granted.
Source: Facebook graph api empty data when getting user likes
Secondly, if the users are not friends on Facebook, the request must also include the parameter appsecret_proof (https://developers.facebook.com/docs/graph-api/securing-requests). So the request must be performed from your server and not from your app for safety reason. Source: https://developers.facebook.com/bugs/101463963524794/
Hope that helps someone one day!

Related

Get Facebook Friends Possible 2020

All the answers that was available are too old tried different ways but not able to get any information related to facebook logged in user friends.
let loginManager = LoginManager()
loginManager.logIn(permissions: ["public_profile", "email", "user_friends"], from: loginScreen) { (result, error) in
if (result?.token != nil) {
self.didLoginWithFacebook()
}
}
the above set "user_friends" permission is not getting asked from the user and also not coming back in granted or denied permission.
And also using
let graphRequest = GraphRequest(graphPath:"me/friends", parameters: ["fields":"id,email,last_name,first_name,picture"], tokenString: accessToken.tokenString, version: nil, httpMethod: .get)
graphRequest.start { (connection, result, error) in
guard let result = result as? [String:String] else{
print("Facebook request user error")
return
}
}
returning empty data fields
I want to know is it even possible to get friends or not ?
Even we don't get friends after asking the user for permission ?
Getting error when trying to login with admin user
According to
https://developers.facebook.com/docs/graph-api/reference/user/friends/
This api returns only
Represents a collection of Users who are friends of the app User. Only
friends who have installed the app and who have granted the app the
user_friends permission will be included.
So, if You don't have any friends who also installed your app and granted friends permission -> response still be empty
Try with an app admin, regular users will not be able to use the permission if it is not approved by Facebook yet
There is a missing apostrophe in your code, right after user_friends:
permissions: ["public_profile", "email","user_friends"...

Unable to link Facebook and Google in Firebase Authentication

I am trying to link Facebook and Google. So, the scenario is this:
I have already authenticated with Google. So, now I am logging in Facebook, having same email id which was used earlier with Google. So, I get the error of account Exists with a different credential. And, I did this:
func fetchUserInfo()
{
Auth.auth().signInAndRetrieveData(with:FacebookAuthProvider.credential(withAccessToken: (FBSDKAccessToken.current().tokenString)!), completion: { (result, error) in
if let error = AuthErrorCode.init(rawValue: error!._code)
{
switch error
{
case .accountExistsWithDifferentCredential :
let credential = FacebookAuthProvider.credential(withAccessToken: (FBSDKAccessToken.current()?.tokenString)!)
Auth.auth().currentUser?.linkAndRetrieveData(with: credential, completion: { (result, error) in
if let error = error
{
print("Unable to link Facebook Account", error.localizedDescription)
}
else
{
NavigationHelper.shared.moveToHome(fromVC: self)
}
})
default: break
}
}
else
{
GeneralHelper.shared.keepLoggedIn()
if let currentUser = Auth.auth().currentUser
{
print(currentUser.email!)
}
NavigationHelper.shared.moveToHome(fromVC: self)
}
})
}
Here Firebase Documentation says that we need to just link the currentUser and retrieve data. But, the issue I am facing is that the currentUser is always nil. So, how can I get the current user? I have already tried this months ago and then I was able to link Facebook, Google and Email. Do, I need to signInAndRetrieve the data from Google in order to get the currentUser?
The Error "account Exists with a different credential" is because, by default, Firebase do not allow to use the same email address for two (or more) different Sing In methods. You need to enable this option.
1 - Go to Authentication > Sign-in method
2 - Scroll down to Advanced: Multiple accounts per email address
3 - Change the option to Allow creation of multiple accounts with the same email address
FYI: You need to do whole login process for each Sign In method in your app. Each method has is own credentials.
Hope this helps.

Facebook all_mutual_friends not working

I'm building an iOS app that connects to Facebook's all_mutual_friends API
https://developers.facebook.com/docs/graph-api/reference/user-context/all_mutual_friends/
The app has been reviewed and approved to connect to this API.
When I make the graph call per the documentation:
let params = ["fields":"context.fields(all_mutual_friends.fields(picture.width(500).height(200)))"]
FBSDKGraphRequest(graphPath: "/\(facebookID)", parameters: params, HTTPMethod: "GET").startWithCompletionHandler { (connection, result, error) in
print("result: \(result)")
print("error: \(error)")
}
I get the following result:
result: {
context = {
id = dXNlcl9jb250ZAXh0OgGQ8qbGU5YUiZAKn8g4zuCb1LCMGj3PiCXfe7B72X5DoKkbMpHZA395xRT0iV8D84NQIZBCjJ3VRKC9uNUrMZCiwIeZBOSTRndZCQKi4pgiIgLoxI3xAZD;
};
id = 10208913134493418;
}
The above response is when we have a mutual friend who has the app installed.
Does anyone know how to get the list of mutual friends?
If I am Facebook friends with the person, I successfully receive a list of mutual friends making the same graph call. This is the response:
context = {
"all_mutual_friends" = {
data = (
{
"first_name" = Mary;
picture = {
data = {
height = 200;
"is_silhouette" = 0;
url = "https://...";
width = 200;
};
};
}, //... 24 more results
This is great because there is data, but it is limited to 25 mutual friends. Is there a way to increase the limit above 25?
According to the Facebook documentation you referred above :
The All Mutual Friends API must be called from your server to obtain the full list of mutual friends. Calling this API in JavaScript, Android, or iOS client code will omit some of the results.
This explains the limit you are facing. :)

get all facebook contacts with swift

I'm trying to get all my facebook contacts. With this code managed to catch however I can not bring the phone information and email contacts. This is my code:
let params = ["fields": "first_name, last_name, name, email, picture, phone"]
let request = FBSDKGraphRequest(graphPath:"/me/taggable_friends", parameters: params);
request.startWithCompletionHandler { (connection : FBSDKGraphRequestConnection!, result : AnyObject!, error : NSError!) -> Void in
if error == nil {
self.appDelegate.dismissHUD()
let resultdict = result as! NSDictionary
self.fbContact = resultdict.objectForKey("data") as! NSArray
print(self.fbContact)
self.loadData()
} else {
self.appDelegate.showError("Time Out")
}
it returns this dictionary:
(
{
"first_name" = xxxx;
id = "AaJRSpI-fYgIaPMaJCHEgCHB59aRecZbRSx0BrvDR_bZV-1W873iXn34kU940C_hc-TaoPnuFDfsJbBYtDSAFWiBfGWBXbxOja0c3NIHhyFqoQ";
"last_name" = xxxx;
name = "xxxx xxxx";
picture = {
data = {
"is_silhouette" = 0;
url = "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfa1/v/t1.0-1/p50x50/10351160_1463351673922060_1213695234585630315_n.jpg?oh=4c3b52137fc70b5743c7d00c2d=56ECD8DC&__gda__=1456979546_a879ab411cdf61e9293f27f70cd43bdd";
};
};
}
)
This is a facebook Graph API limitation. There is some limitations on the data you can get depending on the role you are. You cannot get your friends email or phone using Graph API.
taggable_friends can ONLY be used for tagging, it does not include any other data. You can only get additional data of friends who authorized your App too.
Btw, you can´t even get the phone of the authorized user, there´s no way to get it at all. And for the email, you have to authorize a user with the email permission.
In general, you can´t get any data of users who did not authorize your App, for privacy reasons.

Trouble retrieving list of facebook friends from json data in swift

I am using FBSDK in my Swift iOS application and currently I am trying to retrieve the list of friends who also use my app. I have two-three people who should show up but whenever I search the graphrequest result for friends it returns nil..
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result:AnyObject!, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else
{
//get Facebook ID
let faceBookID: NSString = result.valueForKey("id") as NSString
//get username
let userName : NSString = result.valueForKey("name") as NSString
//get facebook friends who use app
let friendlist: AnyObject = result.valueForKey("friends") as AnyObject
}
I have the "email", "user_friends", and "public_profile" permissions which should be enough to retrieve this information. Any Ideas on what I am doing wrong?? this is doing my head in as my friend doing an android version has successfully got his working..
My problem here was that I never Actually requested for the list of friends in my facebook graph request...
after replacing FBSDKGraphRequest(graphPath: "me", parameters: nil)
with FBSDKGraphRequest(graphPath: "me?fields=id,name,friends", parameters: nil)
I was able to retrieve the list of friends because the response actually contained information for this. A very trivial mistake on my part.
In v2.0 of the Graph API, you must request the user_friends permission
from each user. user_friends is no longer included by default in every
login. Each user must grant the user_friends permission in order to
appear in the response to /me/friends.
see here : https://developers.facebook.com/bugs/1502515636638396/

Resources