How to find Facebook Friends using the same app - iphone

I am creating an app and would like people using the app to find friends using the same app on Facebook. Any help on this would be great.
I know this can be done. I have seen it on Instagram and Tiger Woods 12.

Once you've authorised the user and obtained an access token, you can quickly determine which of their friends have already authorised your app with a call to /me/friends?fields=installed
For the users' friends who have already authorised the app, the result will look like this:
{
"installed": true,
"id": "{USER_ID_GOES_HERE}"
},
and for the users who have not already authorised the app, the result will look like this:
{
"id": "{USER_ID_GOES_HERE}"
},
If you're doing this in the iOS SDK you won't see that exact return value, but the SDK should put the results into a data structure you can use, and check for the presence of the 'installed' item
You can also request additional User object fields other than just 'installed', but this is just a quick example to show you how to get the list of friends that use the app

I have solved this problem in very easy way
Step 1= first download FbGraph API
Step 2= use this code for authentication
NSString *client_id = #"XXXXXXXXXXXXXX";// App id
//alloc and initalize our FbGraph instance
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
//begin the authentication process.....
[fbGraph authenticateUserWithCallbackObject:self andSelector:#selector(fbGraphCallback:)
andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
Step 3= Copy fbGraphCallback: Method from sample code and put in your controller
Step 4= Use this code to get Friends and it will give u the list of friend and who have same app install They look like
{
"installed": true,
"id": "{USER_ID_GOES_HERE}"
},
Code is =
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
[variables setObject:#"installed" forKey:#"fields"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphGet:#"me/friends" withGetVars:variables];
NSLog(#"getMeinstalled Friends: %#", fb_graph_response.htmlResponse);
Thats it!!!!!!!!

I know 'iphone' is tagged in the question, but still this answer might help others who will reach here:
The FQL query to get (just) the ids of all user's friends who are also users of the app is:
SELECT uid FROM user WHERE is_app_user=true AND uid IN (SELECT uid2 FROM friend WHERE uid1 = me())

Related

iOS facebook sdk how to download album, profile photos data

I would like to allow my iPhone App users to view and select from their facebook profile photos, download the photo to use as a profile pic. I am currently using the Facebook SSO SDK and successfully logging in and accessing Graph information. Have tried to access photo information (after successful SSO login) using
[facebook requestWithGraphPath:#"me/albums"];
but I just get an empty data object in return. I've tried using the Facebook API explorer, which is very helpful, and it does give me data for /picture and /photos and /albums. For some reason I don't get the same data using requestWithGraphPath with me/albums, me/picture or me/photos. Just plain "me" works fine but doesn't give me any picture info. Can anybody tell me what I'm doing wrong?
Note: this is the API reference: Facebook Graph API
I did get picture profile to work like this:
[facebook requestWithGraphPath:#"me?fields=picture"];
Update: really I want all of the Profile Photos which is a photo album so I need to know how to access albums through iOS. The profile picture I have successfully obtained now.
I have added permissions "user_photos" and "friends_photos" for photo albums according to API:
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"user_likes",
#"user_birthday",
#"email",
#"publish_stream",
#"publish_actions",
#"user_photos",
#"friends_photos",
nil];
[facebook authorize:permissions];
When I say I'm getting empty data, this is the JSON result (in request:didLoad:) that gets returned:
{
data = (
);
}
From my log file output:
- (void) request:(FBRequest *)request didLoad:(id)result{
NSLog(#"%#",result);
}
Final Note: What finally worked was to use another device, which updated my permissions and then everything started working! My best guess is that the emulator and my iPhone were not updating my facebook permissions so I was being denied access to photos and getting an empty data array.
This SO question was a big help also: How to get photos of a facebook album in iPhone SDK?
For the sake of future viewers, I'm putting the answer to my own question since I haven't received a complete answer from anyone else.
First, you must have the right permissions, either "user_photos" or "friends_photos". Then you have to make sure that your test device updates your permissions -- I think restarting the device is what ultimately did it for me.
Once you've done that, you can get the JSON list of albums, after logging in via SSO, using this call:
[facebook requestWithGraphPath:#"me/albums" andDelegate:self];
This will give JSON array (under key data) with all kinds of information about each album, including the ID. To get a list of photos for a particular album ID (say its '123456789') do the following:
NSString * albumId = #"123456789";
NSString * path = [NSString stringWithFormat:#"%#/photos", albumId];
[facebook requestWithGraphPath:path andDelegate:self];
Your response will of course come via the FBRequestDelegate method request:didLoad where you can process the JSON data.
Download the FBGraph Api documents folder then add it to in your folder. and read the instruction on facebook developer site http://developers.facebook.com/docs/reference/api/.
This is the sample code - sample code
In this you will get a user profile pic in UIAlertView. You can show it where you want.

HTTP 400 Error when accessing Friends list in Facebook Graph API

When I access the list of my friends using the URL https://graph.facebook.com/me/friends?access_token=... I can see the list of my friends. I can do this programmatically as well.
Now, when I take the ID of any of my friends and replace it with "me" in the above URL, and paste the URL in the browser, I can see my friend's friends. I am unable to do this programmatically because it is giving me an HTTP 400 ERROR.
Does anyone know why this is possible by pasting the URL and not programmatically?
You can not get the "friends of friends" using the facebook api.
For example, try the simple /me/friends with the Graph API Explorer, it should work fine.
Then, take one of the ids there and try the same with FRIEND_ID/friends and you should get this:
{
"error": {
"message": "(#604) Can't lookup all friends of FRIEN_ID. Can only lookup for the logged in user or the logged in user's friends that are users of your app.",
"type": "OAuthException",
"code": 604
}
}
The error itself is very straight forward and explains exactly what the problem is.
As for why that translates into a 400 error code for you is unclear.
Edit
You can't see the "friends of friends" in the browser as well, the reason that it works for you (probably) is that the user(s) you check the friends for (USER_ID/friends) installed the app that the access token belongs to, from the way you got to that url I assume that the app is the "Test_console".
How to check? Copy the access token from the url (USER_ID/friends?access_token=xxxxx), go to the Facebook Debugger and paste the token in the text field and click "Debug", it will show you info regarding the application.
The user you checked it for probably has that app "installed", if you check it for other users you will get that error when you get to a user who does not have that app "installed".
I hope this clarifies it for you.
I got friends of friends(limited) who are using application. I had same problem. Though it is very late for answering question, it will help somebody. That's why answering this question.
We can get friends of friends those are app users.
$fb_id= user id whose friends of friends required.
Try this fql query.
$query="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT
uid2 FROM friend WHERE uid1 IN (SELECT uid FROM user WHERE uid IN
(SELECT uid2 FROM friend WHERE uid1 = $fb_id ) and is_app_user=1) )";
It needs following requirements:
Your friend needs to be using application.
Permission from application read_stream, publish_stream, publish_checkins.

Facebook account delink or deauthorize facebook app and check status of linking from facebook app

My website application having feature login with Facebook login. And for which my app is present on Facebook. Login with facebook is working fine.
But application have feature of linking and unlinking facebook account to Facebook app.
How to check FB account is linked with specific FB app or not?
And how to unlink FB account from specific FB app if linked (On that user's request )?
("Linked" means we select "Allow" to FB app on request)
With the new Graph there is a documented way to deauthorize (I've tested it in the Graph API explorer and it works). Send an HTTP Delete command to "me/permissions" with a valid access token. Not only will it invalidate the access token, but it also removes the app from the user's list of auth apps. This requires the user to authorize the app again (going thru the permissions screens) to use it. Happy coding!
Please note Facebook in the process of deprecating the REST API, and have adding equivalent support to the Graph API User object for "auth.revokeAuthorization" method.
//$this->facebook is facebook object
//$userid is of logged in user or can be written hardcoded.
For checking linked in or not by making api call.
$user_id = $this->facebook->getUser();
$result = $this->facebook->api(array(
'method' => 'fql.query',
'query' => "SELECT is_app_user FROM user WHERE uid=$user_id"
));
$is_installed = $result[0]['is_app_user'];
if($is_installed==1) {
echo 'Linked';
}
else {
echo 'Not Linked';
}
For delinking or deauthorization the user by making app call:
$user_id = $this->facebook->getUser();
$access_token=$this->facebook->getAccessToken();
$result = $this->facebook->api(array(
'method' => 'auth.revokeAuthorization',
'uid' =>$user_id,
'access_token'=>$access_token
));
This can easily achieved with simple FQL query:
SELECT uid FROM user WHERE uid = {USER_ID_HERE} AND is_app_user = true
This will return uid of user if he is connected with app and nothing otherwise
Make an API call to /{USER_ID}/permissions with your App access token and it will show if that user has authorised your app
You can also check with the user's access token if you have one.
The response type will be something like this:
{
"data": [
{
"installed": 1,
"bookmarked": 1
}
]
}
There'll be one entry there for each extended permission the user has granted your app and the installed line means they've installed your app
For a user who hasn't installed your app the response will be:
{
"data": [
]
}
You can also use FQL as Juicy Scripter suggests in another answer
Here's how to deauthorize your app using Facebook iOS SDK v4:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
...
NSString *graphPath = [NSString stringWithFormat:#"/%#/permissions", [FBSDKAccessToken currentAccessToken].userID];
[[[FBSDKGraphRequest alloc] initWithGraphPath:graphPath parameters:nil HTTPMethod:#"DELETE"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if ( !error ) {
NSLog(#"Facebook unlinked");
}
}];
Through the Graph API, from the docs (thanks #DMCS):
You can de-authorize an application or revoke a specific extended
permissions on behalf of a user by issuing an HTTP DELETE request to
PROFILE_ID/permissions with a user access_token for that app.
The olds REST API offers you auth.revokeAuthorization. You can read more about that on the auth.revokeAuthorization docs.
Be aware that the REST API will be deprecated in the future, although Facebook doesn't specify when exactly.
I am not aware of any way to get a list of apps a given user is connected to, or see any reason Facebook would make that available. To see if a user is connected to your app, the other answers here should provide you with a couple of ideas.

FQL query to fetch a user's Facebook friend's gender PLUS meeting_sex

Not quite as racy as it sounds - I'm using the below code to get a user's male or female friends only and it works fine. However, I also want to check their 'meeting_sex' which is a list of the genders a user's friend is interested in meeting, i.e. male, female or both. (Described here)
The meeting_sex requires the friends_relationship_details permission which I've added to the graph API explorer, but my output looks like this:
"data": [
{
"name": "ABCDEF",
"pic_square": "https://abcdefg.jpg",
"sex": "male",
"meeting_sex": null,
"uid": 123456789
},
I.e. the meeting_sex is coming back null. I know the meeting_sex is an array, not just a string, as are the other elements, but why is it coming back null?
I'm new to FQL, and in fact any QL, so once I do get the array to read properly, how do I then make the comparison? i.e so I only return MEN who like WOMEN or MEN who like MEN etc. Here's my current code:
NSString *queryString = [NSString stringWithFormat:#"SELECT name, pic_square, sex, meeting_sex, uid FROM user WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) AND sex = 'male' ORDER BY name"];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: queryString, #"query", nil];
[FBRequest getRequestWithParams:dictionary
httpMethod:#"GET"
delegate:self
requestURL:#"fql.query"];
Thanks for any help :)
Michael
I've tried this on my account and I get nulls for all but one of my friends. They have an empty array for both the meeting_for and meeting_sex
I've found that some have suggested this is no longer being used:
http://forum.developers.facebook.net/viewtopic.php?id=103173
Also, it seems there's a lot of privacy concerns around this:
See https://www.facebook.com/settings/?tab=privacy Click "Apps and Websites" then "How people bring your info to apps they use"
Also From https://developers.facebook.com/docs/reference/rest/users.getInfo/
User Privacy and Visible Data
Important: Depending upon the user's privacy settings (including whether the user has decided to opt out of Platform completely), you may not see certain user data. For any user submitted to this method, the following user fields are visible to an application only if that user has authorized that application and specifically chose to reveal them:
meeting_for
meeting_sex
religion
EDIT
New in the Graph API's user (https://developers.facebook.com/docs/reference/api/user/) object:
interested_in
The genders the user is interested in. user_relationship_details or friends_relationship_details. array containing strings.
The doc says is requires user_relashionship_details or friends_relationship_details. I think it would follow that you need to add user_relashionship_details to get this detail about the user itself, not just their friends.
For me i was able to get the data i wanted for "meeting_sex" from the json attribute:
"gender": "male",
"interested_in": ["female"]
Hope this helps.

How do I like a Facebook AppID through IPhone Application?

I am developing a iPhone application in which I want to simply 'Like' my own Facebook AppID.
For Facebook integration I am using FBConnect and Graph API (https://github.com/facebook/facebook-ios-sdk/blob/master/README.mdown).
After researching, I found that I was supposed to use the following method, which I attempted to implement in the "DemoAppViewController.m" of the sample code when the 'publishStream' button was selected:
NSMutableDictionary *params = [NSMutableDictionary dictionary]; //Use an empty dictionary
[_facebook requestWithGraphPath:#"https://graph.facebook.com/163221793740496/likes"
andParams:(NSMutableDictionary *)params
andHttpMethod:#"POST"
andDelegate:(id <FBRequestDelegate>)self];
However, my FBRequest.m 'responseString' indicates: {"error":{"type":"GraphMethodException","message":"Unsupported post request."}}
What am I doing wrong and how can I make this work?
From what I understand, all I need to do is attach my accessToken to https://graph.facebook.com/163221793740496/likes. What exactly does that mean, and how is it accomplished?
Thanks in advance!
P.S. - In case there was any confusion, my post was in reference to having a user sign into Facebook with their account login via a fb like button in the application. After the user allowed the appropriate permissions, my app would 'like' the AppID with the user's account.
You can't programmatically like a page on Facebook. It would be enthusiastically abused.