Facebook Graph API mutual friends images - ios

I'm able to retrieve users' mutual friends using context.fields(mutual_friends)but I need to display images of the friends. However, I'm only getting back the friends id and name only. Is it possible to retrieve their images in the same call?
if((FBSDKAccessToken.current()) != nil){
FBSDKGraphRequest(graphPath: "/\(userID)", parameters: ["fields": "context.fields(mutual_friends)"]).start(completionHandler: { (connection, result, error) -> Void in
if (error == nil){
print("//////Mutual friends")
print(result!)
} else {
print(error!.localizedDescription)
}
})
}

You can get the image with using this API
https://graph.facebook.com/(friendFacebookId)/picture?type=large
For more info check this

Related

Get insights/page_fans for a Facebook page from Facebook's open graph

I'm trying to get the number of fans that a music artist has on their Facebook page, but it's not working. I've combed through the FBAPI docs as well as SO and still nothing. Here's my code:
func getHolychildInfo() {
//Make request
let newGraphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "/holychildmusic/insights/page_fans", parameters: ["period" : "lifetime", "show_description_from_api_doc" : "true", "fields": "read_insights"], httpMethod: "GET")
newGraphRequest.start(completionHandler: { (connection, result, error) in
if ((error) != nil) {
print("Holychild error getting insights: \(error.debugDescription)")
} else {
print("\nHolychild insights result:\n\n\(result)")
}
})
}
Here's my result:
data = (
);
paging = {
next = "https://graph.facebook.com/v2.8/holychildmusic/insights/page_fans?access_token=EAAIB5k3aWEEBAHBD9lZC5AAzZAVV8K8CGBfqaxcrLdZA7oZB2Gdar8cQphXj4VciloZAnZBKp5ZA59BmGloSNz847nFqZCTVsYZCl9rrOk88OnfCnDwwADKnkOO5EUhGumEbW96riHplgfBLdnZAEYmB2Qz4ZAH1sWbuftmGKDqPft4l5QAHSZAimIyI6sOHaKWiurRK201Af6NQCXGliZBsZAUYosUHttkUbo4CQZD&fields=read_insights&format=json&include_headers=false&period=lifetime&sdk=ios&show_description_from_api_doc=true&since=1487457711&until=1487716911";
previous = "https://graph.facebook.com/v2.8/holychildmusic/insights/page_fans?access_token=EAAIB5k3aWEEBAHBD9lZC5AAzZAVV8K8CGBfqaxcrLdZA7oZB2Gdar8cQphXj4VciloZAnZBKp5ZA59BmGloSNz847nFqZCTVsYZCl9rrOk88OnfCnDwwADKnkOO5EUhGumEbW96riHplgfBLdnZAEYmB2Qz4ZAH1sWbuftmGKDqPft4l5QAHSZAimIyI6sOHaKWiurRK201Af6NQCXGliZBsZAUYosUHttkUbo4CQZD&fields=read_insights&format=json&include_headers=false&period=lifetime&sdk=ios&show_description_from_api_doc=true&since=1486939311&until=1487198511";
};
As you can see, there is nothing in the "data" part of the response. The "page_fans" insights metric is supposed to return a number - among other things - but instead returns nothing.
All insights metrics besides the two public ones (page_fans_country and page_storytellers_by_country) require admin access to the page (admin user or page access token with read_insights permission.)
But the fan_count field of the page object is public, so just request that:
https://developers.facebook.com/tools/explorer/?method=GET&path=holychildmusic%3Ffields%3Dfan_count&version=v2.8

Not Getting Own Birthdate from Facebook

I have done lots of R&D to getting private birthday from facebook, but not able to get private birthday.
I have given birthday permission in code. Also tried to delete app from user's account but no luck.
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, birthday, name, first_name, last_name, picture.type(large), email, gender"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
Print("Success")
}else{
Print("Failed")
}
})
}else{
Print("Device token is Nil")
}
Please Help me out where is my mistake.
To access birthdate from facebook, You need to request for
user_birthday permission and submit you app for facebook review. If
facebook will approve this permission, then you can get birthdate.
meanwhile you can test using the account in which you have registered
your app.
Go through
https://developers.facebook.com/docs/facebook-login/permissions#reference-user_birthday

Fetch all fitness data at once

From the official Facebook docs you can get simple statement that you can fetch 3 types of fitness activities:
fitness.bikes
fitness.walks
fitness.runs
That's great but they all share the same fields and there's no possibility to distiguish which one was fetched besides the graphPath.
I need all of this data and ideally I want to fetch it all at once but if I create a method like this:
let walkRequest = FBSDKGraphRequest(graphPath: "me/fitness.walks", parameters: nil)
let runRequest = FBSDKGraphRequest(graphPath: "me/fitness.runs", parameters: nil)
let bikeRequest = FBSDKGraphRequest(graphPath: "me/fitness.bikes", parameters: nil)
var connection = FBSDKGraphRequestConnection()
connection.addRequest(walkRequest) { (connection, result, error) -> Void
//
}
connection.addRequest(runRequest) { (connection, result, error) -> Void
//
}
connection.addRequest(bikeRequest) { (connection, result, error) -> Void
//
}
connection.start()
I get three different callbacs, returning same data and somehow I have to merge this into one structure so I can send it to the backend.
I'd like to get a Dictionary with structure similiar to this:
[
"walks": [ JSON ],
"runs": [JSON],
"bikes": [ JSON ]
]
Is it possible to create a batched request like this? Or do I have to do it manually with 3 different FBSDKGraphRequests and just wait for them to finish?
You should be able to do this via the so-called field expansion:
GET /me?fields=fitness.bikes,fitness.walks,fitness.runs
As I unfortunately have no such data in my account, I can't test the result, but the query runs smoothly.

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/

Facebook friends list api for swift ios

I have tried to get the facebook friends list for an IOS app, but receiving empty response from facebook.
How to get face book friends list in swift?
In Graph API v2.0 or above, calling /me/friends returns the person's friends who installed the same apps. Additionally, 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.
To get the list of friends who are using your app use following code:
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 */
}
}
If you want to access a list of non-app-using friends, then use following code:
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 */
}
}
For fetching the friend list must allow user permission user_friends at the time of login.
For swift 3.0 add below code for fetching friend list:
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
}
}
I hope it works!
Remember only those friends are fetched who are using your app and u should have taken users read permission for user_friends while logging.
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)");
}
}
Refer to : https://developers.facebook.com/docs/graph-api/reference/v2.3/user/friends#read
Just making an union here that solved my problems:
From Dheeraj Singh, to get friends using your app:
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)");
}
}
And to get all facebook friends, from Meghs Dhameliya, ported to 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)");
}
}
you can get friend list using 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
This will only return your facebook friends using the same app.
// 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))")
})
}
With user_friends permission you can have access to the list of your friends that also use your app, so if your app is not used by any of your friend, the list will be empty. See user_friends permission reference

Resources