Facebook oauth/access_token missing - iphone

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.

Related

Graph API iOS picture

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

Facebook Feed Dialog keeps asking for permission

I've been trying to post a link on user's wall, by using Facebook SDK for the iOS.
I'm trying the code below and it works perfectly when I don't have the Facebook App installed. But when I have it, it just ask me to authenticate and send me back to the App. It happens even just to say that I've already gave the necessary permission, but never shows the dialog to do the share.
The code is:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:#"%# %.0f%% OFF", self.offer.description,
[self.offer.price.discountPercent doubleValue] * 100], #"name",
self.offer.store, #"caption",
self.offer.imageKey, #"picture",
#"Test", #"description",
[NSString stringWithFormat:#"http://my_url.com/id%#", self.offer.offerId], #"link",
nil];
[facebook dialog:#"feed" andParams:params andDelegate:self];
Any idea? Thank you in advance ;)
I think this is becouse of you login/logout code. when you making an autorization request to FB, one of params you sending is how long access token must "live". Make shure it lives long anoth. Or, somewhere in the code you invalidating it. May be even losting pointer to access token or something. Make sure, that acces token you use to post is validate at the moment you posting. I suggest you to use official SDK for FB: it's really simple and without any problems. There is great tutorial how to use it: http://developers.facebook.com/docs/mobile/ios/build/

Objective-C/iPhone - oAuthException when using "me/checkins" as part of the Facebook Graph API

I am receiving the following graph response when checking users into Facebook using the Facebook Graph API. I am using the facebook-ios-sdk within my iPhone application to do this.
{"error":{"type":"OAuthException","message":"An unexpected error has occurred. Please retry your request later."}}
I have not made any changes to my code today and I know for a fact (database logs, etc) that their have been successful checkins yesterday.
I'm asking the SO community whether or not they have received similar errors and how they attempted to resolve them?
I have already tried the following:
Revoked access to my application via Facebook.
Reset my offline_access token by forcing Facebook to generate a new one.
Keep in mind my application successfully shares via peoples Facebook walls as well and this is working as expected without any problems.
I request the following permissions from my users:
#"offline_access", #"publish_stream", #"publish_checkins", #"email"
EDIT:
Okay, so this is quite strange and I'm thinking that it's an error on Facebook's end.
I use the following code to tag friends in the checkin:
if ([self.selectedFriends count] > 0) {
[variables setObject:[self.selectedFriends componentsJoinedByString:#","] forKey:#"tags"];
}
fb_graph_response = [fbGraph doGraphPost:#"me/checkins" withPostVars:variables clientId:accessToken];
self.selectedFriends is simply an NSMutableArray of Facebook profile ID's separated by commas, which is what the graph API says to use and remember this has been working fine for months.
http://developers.facebook.com/docs/reference/api/checkin/
If I remove the [variables setObject:[self.selectedFriends componentsJoinedByString:#","] forKey:#"tags"]; then the checkin works just fine for me.
It fails every time I tag one or more friends. Is anyone else receiving the same error when tagging friends in a checkin?
UPDATE 1:
Looks like other people are receiving the same problem relating to Groups:
http://forum.developers.facebook.net/viewtopic.php?pid=349396
I wonder if there's a bug at the moment relating to Checkins.
I've posted a bug:
http://bugs.developers.facebook.net/show_bug.cgi?id=18134
UPDATE 2:
Facebook were able to reproduce the bug and they're now looking into it.
i m using checkins in one of my applications , Regarding your issue ,
Yes , you are right , i had this OAUTH Exception only when sometimes you made checkins continuously with tagging friends,
Also allow permissions like #"user_checkins", #"friends_checkins", in your application
please check this link :
http://tylerwhitedesign.com/how-to-check-in-using-the-facebook-ios-sdk-and-graph-api
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[dictionary objectForKey:#"place"], #"place", //The PlaceID
coordinates, #"coordinates", // The latitude and longitude in string format (JSON)
message, #"message", // The status message
tags, #"tags", // The user's friends who are being checked in
nil];
[_facebook requestWithGraphPath:#"me/checkins" andParams:params andHttpMethod:#"POST" andDelegate: postCheckinRequestResult];
Hope this helps!

iPhone Facebook image upload not working, tried everything

I'm losing my mind over trying to get Facebook SDK for iPhone to upload UIImage.
Here's what I'm doing:
1) Creating facebook property:
facebook = [[Facebook alloc] initWithAppId:#"111111111"];
2) Calling authorize:
[facebook authorize:[NSArray arrayWithObjects:#"publish_stream", nil] delegate:self];
3) Setting params:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, #"picture", nil];
4) Calling post:
[facebook requestWithMethodName:#"photos.upload" andParams:params andHttpMethod:#"POST" andDelegate:self];
When loging in I get the authorize dialog. When trying to upload I receive error 101, Invalid API Key. I've copied the key from FB, I've included it as shown above and I've also included it in .plist (fbAPI_KEY). What the hell am I still missing here? When asked to authorize it did show the name of the app just fine so the key must be correct. I can also post feeds and they appear on users wall, but images just won't upload.
That's a bit weird, I've got the exact same four lines of code which work fine for me. (Although, your app id is a bit shorter than it should be, mine is roughly twice that size).
One thing you may be forgetting about: when are you using each line? You should wait till each of the previous tasks is completed by using the facebook delegates, eg:
- (void)fbDidLogin; //part of the <FBSessionDelegate> protocol
Use these methods to control when the next part of the uploading process should occur, and check for errors along the way. You may be trying to send the photo before the login process has actually finished - use the delegate methods to prevent this.
I think you have to use graph API for uploading images
Check Out:
Ref URL: http://www.raywenderlich.com/1626/how-to-post-to-a-users-wall-upload-photos-and-add-a-like-button-from-your-iphone-app
Hope this helps....

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.)