Facebook API for iOS, Getting friends' profile pictures - facebook

I would like to know how I can fetch friends' profile picture and display them with the Facebook API for iOS
Thanks

One line solution:
https://graph.facebook.com/me/friends?access_token=[oauth_token]&fields=name,id,picture

You can get a list of your friends at this endpoint: https://graph.facebook.com/me/friends. Then, you can get a friend's profile picture at this endpoint: https://graph.facebook.com/[profile_id]/picture.
Be sure to use this SDK: https://github.com/facebook/facebook-iphone-sdk
Hope this helps!

If you use Three20, you can try this extension:
https://github.com/RIKSOF/three20/tree/development/src/extThree20Facebook
It allows you to use all Graph API features with just a few line of code.

for iOS users looking for this, here is what helped me out (using version 5 of the graph, June 2016):
- (void)getUsersFaceBookFriends {
NSDictionary *params = #{#"fields": #"name, id, picture"};
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"/me/friends" parameters:params HTTPMethod:#"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray * friendList = [result objectForKey:#"data"];
for (NSDictionary * friend in friendList) {
NSLog(#"Friend id: %#", friend[#"id"]);
NSLog(#"Friend name: %#", friend[#"name"]);
NSLog(#"Friend picture: %#", friend[#"picture"]);
}
}
}];
}
this code will get the user's Facebook friends who are also using the app, along with their name, id, and profile photo url.

Related

What is the way to upload the actual username from PFFacebookUtils to the Parse Cloud?

I have my [PFFacebookUtils logInWithPermissions] like this:
NSArray *permissionsArray = #[#"user_about_me"];
[PFFacebookUtils logInWithPermissions:permissionsArray block:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(#"Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew) {
} else {
NSLog(#"User logged in through Facebook!");
} }];
The problem is that the method uploads an username similar to "St42o7PejDRh6scvMJylgOxGQ". What i want is to have the real username from Facebook uploaded, as well as the Facebook password and other relevant fields.
EDIT : With username i am specifically meaning First name, last name, and email if possible !
The Graph API uses OAuth as auth mechanism, so there's no way to get the username and password.
See
https://developers.facebook.com/docs/facebook-login/overview/v2.3
https://developers.facebook.com/docs/facebook-login/access-tokens
Not sure if this is still an issue for you, but because Facebook removed this feature from their API you can't access it like you can for, let's say a PFUser
example:
NSString *userName = [[PFUser user] username];
What you CAN do however is open up a Facebook Graph API request in order to retrieve this information like such:
1) First set the parameters of what you want to get from Facebook: (here i just did the FB id, the username, and the email)
NSMutableDictionary* parameters = [NSMutableDictionary dictionary];
[parameters setValue:#"id,name,email" forKey:#"fields"];
2) Just call the Facebook Graph API and request the information that you need like such:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:#"me" parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(#"Facebook Graph API user: %#", result);
}
}];
3) This is what you should get: (I have replaced personal information with generic pointers for security reasons)
2015-11-15 18:52:18.204 YOURAPPNAME[60800:18917239] Facebook Graph API user: {
id = <the user id>;
name = "Full User Facebook Name";
email = "user#email.com";
}
Hopefully this will help you get what you are looking for! If its too late, at maybe this will help someone else in the future

Post on Facebook Page from iOS App Not Showing Up on Page?

I am posting a picture and dummy text on a facebook page with the Page Id "848190045198980".I am signing in using my facebook credentials.Also I have admin role for the specific page.So I am just getting the basic permissions from facebook.Below is the request and the response.
Request :
UIImage * productImage = [UIImage imageNamed:#"corporate.png"];
NSData * imageData = UIImagePNGRepresentation(productImage);
NSDictionary *params = #{#"message": #"Hello XYZ", #"source":imageData};
[FBRequestConnection startWithGraphPath:#"/848190045198980/photos" parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error)
{
if(completionBlock)
{
NSLog(#"%#",result);
completionBlock(YES,result,nil);
}
}
else
{
if(completionBlock)
completionBlock(NO,nil,error);
}
}];
Response :
{"id":"1481175438805031","post_id":"848190045198980_1481175438805031"}
Expected Result : My Post should show up on the Facebook Page in "Posts to Page" Section.
Actual Result : Nothing Shows Up.
Please point me in the right direction as to what I am missing here.I have checked the API Doc several times and I am finding it real tough as to where I am missing something.
Regards,
Roman
This is how I am posting video on Facebook:
NSDictionary *params = #{
#"message":message,
#"tags":Mystring,
#"link":#"http://www.miimobileapp.com/website/index.html",
#"privacy": #"{'value': 'SELF'}",
};
/* make the API call */
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,id result,NSError *error) {
NSLog(#"Post successful");
}

Tagging a friend to the wall post using graph API in iOS

I am using this code to post on my wall and at the same time I want to tag to my specefic friends but don't know how I am using Fbgrapgh API please help me.
[variables setObject:messageTextView.text forKey:#"message"];
[graphref doGraphPost:[NSString stringWithFormat:#"me/feed",self.friendID] withPostVars:variables];
Thanks
To tag someone you have to use FbDialogs. you cannot tag your friends in status update in any way other than FB dialogs.
to share status using FBDialog refer this sample code
[FBDialogs presentShareDialogWithLink:nil handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
NSLog(#"Error: %#", error.description);
} else {
NSLog(#"Success!");
}
}];
it is taken from here https://developers.facebook.com/ios/share-dialog/#statusupdate

Facebook iOS SDK 3.0: how to make comment to existing URL?

Has anyone tried to use the new Facebook SDK to post a comment to an URL? i tried by using startForPostWithGraphPath like this (code snippet picked from Scrumptious project):
id action = [FBGraphObject graphObject];
[action setValue:#"test from iOS app" forKey:#"message"];
// Create the request and post the action to my url path.
[FBRequestConnection startForPostWithGraphPath:#"XXXXXXXXXXXXXXXXXXX/comments"
graphObject:action
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error)
{
NSLog(#"error: %#", error.localizedDescription);
}
else
{
NSLog(#"ok!!");
}
}];
it doesn't work neither from Open Graph API explorer. The error i receive is "an unknown error has occurred", code 200. The XXXXXXXX url is the ID of the page i have comments on, i can read them, but not correctly post to them. Maybe the open graph path is wrong?
Hey Friend It's a Good News for you.
I Post the comments and Likes with GraphAPI in IOS
Just write Some code below.
NSMutableDictionary *res = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"This is my comment", #"message", #"Your api's access Token",#"access_token",nil];
[FBRequestConnection startWithGraphPath:#"XXXXXXXXXX/comments" parameters:res HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error)
{
NSLog(#"error: %#", error.localizedDescription);
}
else
{
NSLog(#"ok!!");
}
}];
XXXXXXXXXX = Id where you want to comment.
Note:-
Please do not appent https://graph.facebook.com
before
XXXXXXXXXX/comments

Post photo on user's wall using Facebook iOS SDK

I'm trying to upload a photo from the camera to a user's Facebook wall. I'm not entirely sure what the correct strategy is, but from reading around it seems the thing to do is upload the photo to an album, and then someone post on the wall a link to that album/photo. Ideally this would involve the dialog, but from what I can tell that's not possible.
I've managed to upload a photo to an album, and get back an ID for that photo, but I'm not sure what to do after that.
Can anyone provide some straightforward code for achieving this?
Bonus question: Is it possible to post the photo to the application wall, as well (or instead)?
Edit: Graph API is preferable, but anything that works at this stage is good.
I did this in three steps
1 post picture to album (returns imageID)
2 use imageID to request metadata for imageID
3 use the 'link' field (not the 'source' field) as a link to the image in a post to the user's wall
The downside is that there are now two posts to the wall, one for the image, and one for the actual post. I haven't figured out yet how to post a picture to an album, without also a wall post appearing (ideally it would just be the 2nd post that appears)
Step 1:
- (void) postImageToFacebook {
appDelegate = (ScorecardAppDelegate *)[[UIApplication sharedApplication] delegate];
currentAPICall = kAPIGraphUserPhotosPost;
UIImage *imgSource = {insert your image here};
NSString *strMessage = #"This is the photo caption";
NSMutableDictionary* photosParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
imgSource,#"source",
strMessage,#"message",
nil];
[appDelegate.facebook requestWithGraphPath:#"me/photos"
andParams:photosParams
andHttpMethod:#"POST"
andDelegate:self];
// after image is posted, get URL for image and then start feed dialog
// this is done from FBRequestDelegate method
}
Step 2 (kAPIGraphUserPhotosPost) & step 3 (kAPIGraphPhotoData):
- (void)request:(FBRequest *)request didLoad:(id)result {
if ([result isKindOfClass:[NSArray class]] && ([result count] > 0)) {
result = [result objectAtIndex:0];
}
switch (currentAPICall) {
case kAPIGraphPhotoData: // step 3
{
// Facebook doesn't allow linking to images on fbcdn.net. So for now use default thumb stored on Picasa
NSString *thumbURL = kDefaultThumbURL;
NSString *imageLink = [NSString stringWithFormat:[result objectForKey:#"link"]];
currentAPICall = kDialogFeedUser;
appDelegate = (ScorecardAppDelegate *)[[UIApplication sharedApplication] delegate];
NSMutableDictionary* dialogParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppId, #"app_id",
imageLink, #"link",
thumbURL, #"picture",
#"Just Played Wizard etc etc", #"name",
nil];
[appDelegate.facebook dialog:#"feed"
andParams:dialogParams
andDelegate:self];
break;
}
case kAPIGraphUserPhotosPost: // step 2
{
NSString *imageID = [NSString stringWithFormat:[result objectForKey:#"id"]];
NSLog(#"id of uploaded screen image %#",imageID);
currentAPICall = kAPIGraphPhotoData;
appDelegate = (Scorecard4AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.facebook requestWithGraphPath:imageID
andDelegate:self];
break;
}
}
}
I've modified the code to show just the Facebook stuff condensed. If you want to check if the post is successful you'll want something like this:
- (void)dialogDidComplete:(FBDialog *)dialog {
switch (currentAPICall) {
case kDialogFeedUser:
{
NSLog(#"Feed published successfully.");
break;
}
}
}
The blue text in the Facebook post is whatever you put in the "name" parameter in Step 3. Clicking on the blue text in Facebook will take you to the photo posted in Step 1, in an album in Facebook (Facebook creates a default album for your app if you don't specify an album). In my app's case it's the full-sized image of the scorecard (but could be any image, e.g. from the camera). Unfortunately I couldn't figure out a way to make the thumb image a live link, but it's not very readable so a default thumb works in my case. The part in the Facebook post (in black font) that says "First game of the year..." is entered by the user.
It's clearer what you wish to do - post a photo to FB, and guarantee that a post goes on the user's wall/stream.
Unfortunately, there are some things in the way.
FB Graph API appears to only allow you to post EITHER a picture to an album, or post to the wall directly, linking to a picture already existing somewhere on the web. In the first case, a post in the stream will probably be made, but FB appears to consolidate multiple posts in some manner so as to keep the user's stream from being bombarded. The mechanism for this is not documented anywhere I could see.
In the second case, you might think you could get away with posting to an album, and then explicitly posting a link to the album. You can add a parameter to the original album post, "no_story" with a value of 1, and suppress the wall post that might be made while you prepare to make an explicit one. However, FB will not have the source URL for a newly posted image for a while, AND, it doesn't appear to like URLs that include its own content delivery network, returning an error. You might think to simply put status update in the stream, talking about the post, However, the Graph API is also limited to 25 such direct feed posts per day per app, to prevent spamming.
One solution would be to post to something like Flickr, get the URL of the image, and then post to the wall. FB's preferred solution appears to be to use the FB dialogs that are part of the mobile toolkit - essentially little web pages much like the OAuth screen.
Personally, I plan to simply post to the album as above, and live with FB's idea of how the user should be notified. Curious how you choose to proceed.
I'm not sure what part isn't working for you, since you are posting and getting an ID back, but here is what I did in a quick and dirty way, in case someone reaches here via Google.
This is an HTTP POST function, and the binary data of the file goes up as multipart mime.
I'm a big fan of the ASIHTTPRequest library available here.
**UPDATE: 10/22/2012 ** - AFNetworking has replaced ASIHTTPRequest in my code in the past few months. Available on GitHub here
Facebooks docs are confusing, partly because they are incomplete and partly because they can be wrong. You'll probably tear some hair out figuring out exactly what post value to set for a caption or something, but this recipe puts a photo into an album, and that goes into the feed.
You still need to set up the Facebook OAuth stuff in the basic way - I happened to do that in the app delegate, so I grab the Facebook object from there to get my access token. I made sure to ask for the "publish_stream" permission when I authenticated, like this:
[facebook authorize:[NSArray arrayWithObjects:#"publish_stream", nil] delegate:self];
This will create or add to an album called "YOUR_APP_NAME Photos", and will appear in the user's feed. You can put it in any album, including the "Wall" album, by getting the ID of that album and changing the URL to http://graph.facebook.com/THE_ID_OF_THE_ALBUM/photos.
Here's the basic method:
-(void) postImageToFB:(UIImage *) image
{
NSData* imageData = UIImageJPEGRepresentation(image, 90);
Facebook* fb = [(uploadPicAppDelegate *)[[UIApplication sharedApplication] delegate] facebook ];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]];
[request addPostValue:[fb accessToken] forKey:#"access_token"];
[request addPostValue:#"image message" forKey:#"message"];
[request addData:imageData forKey:#"source"];
[request setDelegate:self];
[request startAsynchronous];
}
Using the Facebook provided iOS library looks like this:
-(void) postImageToFB:(UIImage *) image
{
NSData* imageData = UIImageJPEGRepresentation(image, 90);
Facebook* fb = [(uploadPicAppDelegate *)[[UIApplication sharedApplication] delegate] facebook ];
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:[fb accessToken],#"access_token",
#"message text", #"message",
imageData, #"source",
nil];
[fb requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
Using Facebook SDK 3.0:
- (void)postPhotoThenOpenGraphAction {
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
// First request uploads the photo.
FBRequest *request1 = [FBRequest
requestForUploadPhoto:self.selectedPhoto];
[connection addRequest:request1
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
}
}
batchEntryName:#"photopost"
];
// Second request retrieves photo information for just-created
// photo so we can grab its source.
FBRequest *request2 = [FBRequest
requestForGraphPath:#"{result=photopost:$.id}"];
[connection addRequest:request2
completionHandler:
^(FBRequestConnection *connection, id result, NSError *error) {
if (!error &&
result) {
NSString *source = [result objectForKey:#"source"];
[self postOpenGraphActionWithPhotoURL:source];
}
}
];
[connection start];
}
They follow this post with an OpenGraph action publish ([self postOpenGraphActionWithPhotoURL:source];), but if you just want the image on the user's wall, you wont need that.
More info:
https://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/publish-open-graph-story/#step7
Yay!, FB SDK 3.0 rocks! No more AppDelegate.facebook :)
I searched far and wide for a solution that worked on the latest APIs, until I came across this:
http://xcodenoobies.blogspot.co.uk/2012/09/how-to-upload-photo-and-update-status.html
By far the clearest solution I've come across, simple and works with the latest API.