Graph API iOS picture - iphone

I have a graph api call from my ios application. It looks something like this:
[[appDelegate facebook] requestWithGraphPath:[NSString stringWithFormat:#"%i/picture", [[userData objectForKey:#"id"] intValue] andDelegate:self];
(might be some typos)
But in the request did load when i nslog the result, it is (null). Graph API says that you need to use fields=picture or something to get the url. How would I do this? If it is possible could somebody show me some example code?

You don't need an actual Graph API call to load a user's facebook picture. Simply construct a url like this, and it will return the specified user's facebook image:
NSURL *fbPictureURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://graph.facebook.com/%#/picture", [userData objectForKey:#"id"]];
You can then do with that what you want, like load it in an UIImageView.

See my answer on this post to see how to get the picture URL using requestWithGraphPath:
iOS Facebook Graph API Profile picture link

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.

Facebook iOS Integration

I have recently implemented the Facebook functionality to post an URL as explained in iOS Facebook Development
It's the same as explained.
After I've signed up on Facebook and set my app key, I started the iPhone simulator and authorized as explained on Facebook. After that the Mobile Safari didn't jump back to my app (Because I don't want Facebook to jump back. Later I want to do that calling a webpage by my own). So I just started it again (activated because of iOS 4.2).
After that, I switched to the where I post a default URL.
NSMutableDictionary *params = [NSMutableDictionary dictionary];
NSString *communityURL = #"http%3A%2F%2Fwww.google.com";
[params setObject:communityURL forKey:#"link"];
[facebook dialog:#"feed" andParams:params andDelegate:self];
Normally it should bring up a dialog with this parameters, but it just showed an error:
This page contains the following errors:
error on line 25 at column 35: xmlParseEntityRef: no name
Below is a rendering of the page up to the first error.
When I debug into the method which calls the dialog, I got the URL, which will be called.
When executing this URL in a web browser, it works.
Why doesn't it work?
What's wrong?
i didn't figure out what it was, but i came up with an other solution.
I now use ShareKit. It's easy to use and it handles all alone. I try to post some sourcecode snipets asap.
For those, who have the same problem and don't want to use another API, try to do what ever the facebook documentation tells you.
Br
Nic
ok what I did is simply add
NSURL *url = [NSURL URLWithString: #"http://www.google.com"];
SHKItem *item = [SHKItem URL:url title:#"Share Item with Google url ;)"];
// Get the ShareKit action sheet
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// Display the action sheet
[actionSheet showFromTabBar:self.tabBarController.tabBar];
And that's what I do.
If someone needs help, please feel free!!
Br
Nic

How to use FBConnect dialog method to post a UIImage to my wall?

(Note: I've looked all over for this, and there are tons of examples for how to post a URL-to-image via dialog, and a few for how to post image-data direct to photo album, but neither of those are what I'm trying to do.)
I can post an image that's stored on the web easily enough with code similar to this:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"This is what I say about kittens!", #"message",
#"Kittens!", #"name",
#"I'm feeling fuzzy", #"caption",
#"You, too, can feel fuzzy, with FuzzyKitten™", #"description",
#"http://example.com/fuzzykitten.html", #"link",
#"http://placekitten.com/200/150", #"picture",
nil];
[facebook dialog: #"feed"
andParams: params
andDelegate: self];
And everything works fine. The problem is, I'd like to replace:
#"http://placekitten.com/200/150", #"picture",
with something along the lines of:
[UIImage imageNamed: #"myImage.png"], #"picture",
which, of course, isn't right. So my question is: If I have an image in UIImage form, how do I load that into the parameters dictionary to pass to the dialog method?
Thanks!
EDIT: This is a client iPhone app for which there is no server so, while uploading the image to FB as part-1 of a 2-part process is fine, I'm now not seeing how to find the uploaded image's URL. Thanks again!
You can't directly upload an image to the wall, when publishing to the wall you can just link an image to it.
So, you need to upload the UIImage somewhere first. You have 2 options:
1. Upload to your/some server and publish a wall post linking to it.
2. Upload to a facebook album. Check the graph API about doing that, it is pretty straightforward.
edit: you can no longer source images hosted in facebook CDN (and probably other CDNs), so if you need to link an image from a dialog you should upload it on a server and get a permalink from there
You could use ASIFormDataRequest and run a script to upload your file to your server using something like this:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
// Upload a file on disk
[request setFile:#"/Users/ben/Desktop/ben.jpg" withFileName:#"myphoto.jpg" andContentType:#"image/jpeg"
forKey:#"photo"];
Then have the ASIFormDataRequest's response return the url of the uploaded image to your application.
You can use the event of the ASIFormDataRequest response to trigger the Facebook image post.

Using 'Like' with the Facebook Graph API on iOS

I am using the latest Facebook SDK for iOS and Graph API. Works Great.
I'm trying to 'Like' status updates, doing the following:
NSManagedObject *managedObject = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSString *messageID = (NSString*)[managedObject valueForKey:#"message_id"];
NSLog(#"Like: %#", [NSString stringWithFormat:#"%#/likes", messageID]);
[facebook requestWithGraphPath:[NSString stringWithFormat:#"%#/likes", messageID] andParams:nil andHttpMethod:#"POST" andDelegate:self];
However this returns an error from Facebook:
facebookErrDomain error 10000.
Anyone know why this is?
Figured it out. You cannot pass nil to the parameters. So I created an empty NSMutableDictionary, and passed that. Worked fine. (By the way: it’s not possible to like a page this way.)

Facebook oauth/access_token missing

Dunno if Im missing something but here goes. Im trying to get an access_token for my application so that it can go and look up events for certain public groups on facebook, without requiring a user to be logged in.
Im trying to get an access_token from
https://graph.facebook.com/oauth/access_token?client_secret=foobar&client_id=foobar&grant_type=client_credentials&format=json&sdk=ios
This returns a string: access_token=xxxx|ugtqdoWfvvo5_S-Fo2D7_I4rdbc
Thats nice and all, but its no json. Any insights on why the returned string is not json encoded ?
Note:
Im using the Facebook ios SDK function like so
[_facebook requestWithGraphPath:#"oauth/access_token" andParams:params andHttpMethod:#"POST" andDelegate:self];
This is a BUG: https://developers.facebook.com/bugs/325262010847554
Facebook's OAuth 2.0 implementation is clearly in violation of the spec: https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-25#section-5.1
I'm using Server-side Web App flow and have the same issue that you describe for the iOS SDK.
This is fixed as of v2.3 of the graph api. Specify the version by using endpoint https://graph.facebook.com/v2.4/oauth/access_token.
It's easy to accidentally land on the wrong version of the facebook API docs.
Here's a snippet to extract your access token and replace the pipe character with the proper escape...
NSURL *accessTokenURL = [NSURL URLWithString:#"https://graph.facebook.com/oauth/access_token?client_id=XXXXXXXXXXXXX&client_secret=XXXXXXXXXXXXXXXXXXX&grant_type=client_credentials"];
NSString *accessTokenResponse = [NSString stringWithContentsOfURL:accessTokenURL encoding:NSUTF8StringEncoding error:NULL];
if(accessTokenResponse != nil) {
NSMutableString *accessToken = [NSMutableString stringWithString:[accessTokenResponse substringFromIndex:13]];
[accessToken replaceOccurrencesOfString:#"|" withString:#"%7C" options:NSLiteralSearch range:NSMakeRange(0, [accessToken length])];
}
Follow up.
The way I understand the FB ios SDK is that your delegate function will receive an object such as NSArray or NSDictionnary to work with. Looking into the internals of the FB SDK classes it seems that all response are parsed using the SBJSON parser. If the response is not json than that will fail.
[_facebook requestWithGraphPath:#"261247595170" andDelegate:self];
returns nice json. No problems there.
[_facebook requestWithGraphPath:#"oauth/access_token" andParams:params andHttpMethod:#"POST" andDelegate:self];
String returned. SBJSON parser fails.
This seems to be an inconsistency in the FB graph service.
One could write a special function to handle this problem, but if FB decides to change the format of the string, then all IPhone App would stop working, and need to update. Not cool.
If you're using Facebook SDK, I believe that they return NSArray and NSDictionnary
I might be wrong, but try with an objectForKey:#"access_token"
Any insights on why the returned
string is not json encoded ?
Yeah, the access token isn't supposed to be JSON-encoded. And there's arguably little value in JSON-encoding a single string anyway.
Why are you expecting it to be so?
Seems I was misinformed about this. The spec states that application/json is to be used for the response body.