facebookErrDomain error 10000 when calling graph api Facebook ios - iphone

I'm struggling with the graph api for a few days now and I can't seem to get any further.
In my appdelegate i've placed the following code within the didFinishLaunching
facebook = [[Facebook alloc] initWithAppId:#"cut-app-id-out"];
NSArray* permissions = [[NSArray arrayWithObjects:
#"email", #"user_location", #"user_events", #"user_checkins", #"read_stream", nil] retain];
[facebook authorize:permissions delegate:self];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"test message from stackoverflow", #"message",
nil];
[facebook requestWithGraphPath:#"/me/feed" andParams:params andDelegate:self];
as you can see i'm trying to get the users feed out. I use the following code to place the data into a proper dictionary.
- (void)request:(FBRequest*)request didLoad:(id)result
{
if ([result isKindOfClass:[NSDictionary class]]) {
NSLog(#"count : %d ", [result count]);
NSArray* resultArray = [result allObjects];
NSLog(#"count : %d ", [result count]);
result = [resultArray objectAtIndex:0];
NSLog(#"count : %d ", [result count]);
result = [result objectAtIndex:0];
}
}
But the didFailWithError: function gives me the following error:
The operation couldn’t be completed. (facebookErrDomain error 10000.)
I've place the FBRequestDelegate in my interface file as following:
#interface TabbedCalculationAppDelegate : NSObject
<FBRequestDelegate>{
Is there something what i'm missing?

There's a lot more needed to handle Facebook responses, especially after authorization. You're not going to be able to make calls against the Facebook API immediately after the authorize method. Please see their sample code which shows how to respond to the various events which you will need to do, most importantly the fbDidLogin and fbDidNotLogin ones. Only once you have a successful login should you access the Graph API.
In addition, it looks like you're trying to post a message with the Graph API. Unfortunately that's not the way to do it and a call like [facebook requestWithGraphPath:#"me/feed" andDelegate:self]; is meant for read-only use. Again, look at the above link for an example of how to use the publish_stream permission (their code has a publishStream example method).

Related

Publish Feed with SSO in Facebook (IOS)

I am working on Facebook integration and trying for Single Sign-On with publish feed functionality.
I am using latest FacebookSDK. I have Facebook's Hackbook example code but, i am new to all this so it is being difficult to understand completely all this things.
While searching on SSO i got some code, It is working fine. Here is the code i am using (At the end of this page there is a source code attached)
FBUtils.h and FBUtils.m class
ViewController.m
- (IBAction)publishFeed:(id)sender {
//For SSO
[[FBUtils sharedFBUtils] initializeWithAppID:#"3804765878798776"];
NSArray *permision = [NSArray arrayWithObjects:#"read_stream",#"publish_stream", nil];
[[FBUtils sharedFBUtils] LoginWithPermisions:permision];
[FBUtils sharedFBUtils].delegate = self;
FBSBJSON *jsonWriter = [FBSBJSON new];
/// for publishfeed
NSArray* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
#"Get Started",#"name",#"https://itunes.apple.com?ls=1&mt=8",#"link", nil], nil];
NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
// Dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"I have lot of fun preparing.", #"name",
#" exam", #"caption",
#" ", #"description",
#"https://itunes.apple.com", #"link",
#"http://mypng", #"picture",
actionLinksStr, #"actions",
nil];
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[delegate facebook] dialog:#"feed"
andParams:params
andDelegate:self];
When i tap Facebook button in my app it is redirect me to Facebook and then retuning back to my app. Now , what i want is to fire publishFeed event right after returning back to the app and it should ask direct for post or cancel options to the user. But it is asking for login again like this.
Can any one help me in this or please suggest me the right way.
Your Suggestions would be a great help.
In your method, you're not checking if the app has permissions to publish post and if the user logged in before. So, every time you call this method, the app wants you to login. I think that is the problem.
If I'm right, you need to add permission and login control in your method like this. This is my sample code from another project, you can get the logic behind it.
- (IBAction)facebookShare:(id)sender
{
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// You check the active session here.
if (FBSession.activeSession.isOpen)
{
// You check the permissions here.
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession
reauthorizeWithPublishPermissions:
[NSArray arrayWithObject:#"publish_actions"]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
[self postFacebook];
[[[UIAlertView alloc] initWithTitle:#"Result"
message:#"Posted in your wall."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
}
}];
} else {
// If permissions present, publish the story
[self postFacebook]; // ------> This is your post method.
[[[UIAlertView alloc] initWithTitle:#"Sonuç"
message:#"Duvarında paylaşıldı."
delegate:self
cancelButtonTitle:#"Tamam"
otherButtonTitles:nil]
show];
}
}
else
{
// If there is no session, ask for it.
[appDelegate openSessionWithAllowLoginUI:YES];
}
// NSLog(#"Post complete.");
}

Facebook Graph API for iOS search

I am trying to search places from the GraphAPI using following code without luck. Can anybody please enlight my path ?
If I try to post link/message/photo it works as expected but when trying to get location it always fails and gives me **The operation couldn’t be completed. (facebookErrDomain error 10000.)**
//Following statement is using permissions
NSArray * permissions = [NSArray arrayWithObjects:#"publish_stream",#"user_checkins", #"friends_checkins", #"publish_checkins", nil];
[facebook authorize:FB_APP_ID permissions:permissions delegate:_delegate];
NSString *centerString = [NSString stringWithFormat: #"%f,%f", 37.76,-122.427];
NSString *graphPath = #"search";
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"coffee",#"q",
#"place",#"type",
centerString,#"center",
#"1000",#"distance", // In Meters (1000m = 0.62mi)
nil];
[facebook requestWithGraphPath:_path andParams:_params andHttpMethod:#"POST" andDelegate:_delegate];
Never mind. Downloaded latest sample HackBook from facebook for graph api from github and it includes sample code for the same.
For "search" you should use "GET" instead of "POST".
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#search
With Facebook iOS SDK, you can use FBRequestConnection after login.
[FBRequestConnection startWithGraphPath:#"search?q=coffee&type=place&center=37.76,-122.427&distance=1000"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Sucess! Include your code to handle the results here
NSLog(#"result: %#", result);
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"error: %#", error);
}
}];
With Last SDK
NSMutableDictionary *params2 = [NSMutableDictionary dictionaryWithCapacity:3L];
[params2 setObject:#"37.416382,-122.152659" forKey:#"center"];
[params2 setObject:#"place" forKey:#"type"];
[params2 setObject:#"1000" forKey:#"distance"];
[[[FBSDKGraphRequest alloc] initWithGraphPath:#"/search" parameters:params2 HTTPMethod:#"GET"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
NSLog(#"RESPONSE!!! /search");
NSLog(#"result %#",result);
NSLog(#"error %#",error);
}];

Fetching Facebook friends with Graph API

I am trying to retrieve the list of facebook friends of a logged user in an Iphone app.
this is the code I am using
[_facebook requestWithGraphPath:#"me/friends" andDelegate:self];
the result I get is a NSDictionary instance with only an object inside.
Am I doing something wrong?
You parse your DIctionary Data.
Sample code here,
if ([result isKindOfClass:[NSDictionary class]]) {
NSArray *userInfoArray=[result objectForKey:#"data"];
NSMutableArray *userIdArray=[[NSMutableArray alloc ]init];
NSMutableArray *userNameArray=[[NSMutableArray alloc ]init];
for (int indexVal=0; indexVal<[userInfoArray count]; indexVal++) {
NSDictionary *individualUserInfo=[userInfoArray objectAtIndex:indexVal];
[userIdArray addObject:[individualUserInfo objectForKey:#"id"]];
[userNameArray addObject:[individualUserInfo objectForKey:#"name"]];
}
NSLog(#"Frnd Name Array : %# ",userNameArray);
NSLog(#"Id Array : %# ",userIdArray);
}
Here result is the data you received in request:DidLoad delegate method

How to publish from iOS application to facebook wall without user amending message

I'm writing an iPhone game and want to publish the user's score to their facebook feed.
I've managed to knock together an example where the user agrees with the authorisation and then a dialog appears which they can confirm to publish on their wall, or not publish. This is almost ideal, except that the text is an editable field - so the user could amend their score and then publish. Ideally, I want the exact same mechanism, but without the ability to amend the message.
I'm assuming to do this, I would need to ask publish_stream permissions, followed by a Graph api call to post the message. I sourced this, but get an error 'An active access token must be used to query information about the current user.'.
I'll happily take a point in the right direction over the actual code change - any help much appreciated.
This is my first stackOverflow post, so be gentle please.
Thanks guys.
-Duncan
Original code (which publishes to wall but with amendable textbox)
//offer to facebook connect your score
facebook = [[Facebook alloc] initWithAppId:#"210645928948875"];
[facebook authorize:nil delegate:self];
NSMutableString *facebookMessage = [NSMutableString stringWithString:#"I scored a whopping "];
[facebookMessage appendString: [NSMutableString stringWithFormat:#"%d", currentScore]];
[facebookMessage appendString: [NSMutableString stringWithString:#". Can you beat me?"]];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"210645928948875", #"app_id",
#"http://duncan.co.uk/", #"link",
#"http://d.yimg.com/gg/goran_anicic/dunc.jpeg", #"picture",
#"dunc", #"name",
//#"Reference Documentation", #"caption",
#"Download the app NOW from the App Store", #"description",
facebookMessage, #"message",
nil];
[facebook dialog:#"stream.publish" andParams:params andDelegate:self];
Code to publish direct to wall (not proved) (which raises active token error):
/*Facebook Application ID*/
NSString *client_id = #"210645928948875";
//alloc and initalize our FbGraph instance
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
//begin the authentication process..... andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access"
[fbGraph authenticateUserWithCallbackObject:self andSelector:#selector(fbGraphCallback:) andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access"];
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
[variables setObject:#"the message" forKey:#"message"];
[variables setObject:#"http://duncan.co.uk" forKey:#"link"];
[variables setObject:#"bold copy next to image" forKey:#"name"];
[variables setObject:#"plain text score." forKey:#"description"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"me/feed" withPostVars:variables];
NSLog(#"postMeFeedButtonPressed: %#", fb_graph_response.htmlResponse);
Solution found. The authentication should be called first. Once authenticated, this will call the fbGraphCallback function which will perform the post the the users stream.
All of this was made possible courtesy of http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api. And bonus points goes to The Mad Gamer. Thanks a lot.
-(IBAction)buttonPublishOnFacebookPressed:(id)sender {
/*Facebook Application ID*/
NSString *client_id = #"123456789012345";
//alloc and initalize our FbGraph instance
self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];
//begin the authentication process.....
[fbGraph authenticateUserWithCallbackObject:self andSelector:#selector(fbGraphCallback:)
andExtendedPermissions:#"publish_stream" andSuperView:self.view];
}
-(void)fbGraphCallback:(id)sender {
if ((fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0)) {
NSLog(#"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");
} else {
NSLog(#"------------>CONGRATULATIONS<------------, You're logged into Facebook... Your oAuth token is: %#", fbGraph.accessToken);
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];
[variables setObject:#"http://farm6.static.flickr.com/5015/5570946750_a486e741.jpg" forKey:#"link"];
[variables setObject:#"http://farm6.static.flickr.com/5015/5570946750_a486e741.jpg" forKey:#"picture"];
[variables setObject:#"You scored 99999" forKey:#"name"];
[variables setObject:#" " forKey:#"caption"];
[variables setObject:#"Download my app for the iPhone NOW." forKey:#"description"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"me/feed" withPostVars:variables];
NSLog(#"postMeFeedButtonPressed: %#", fb_graph_response.htmlResponse);
//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];
[parser release];
NSLog(#"Now log into Facebook and look at your profile...");
}
[fbGraph release];
}
It looks like your permissions are a comma separated NSString, not an NSArray. The FB authorize: expects an NSArray, not an NSString.
Are you waiting for the response from the authenticate that calls fbGraphCallback ( is that code paraphrased?) I'm guessing your callback would show an auth failure because your perms are invalid. You have to wait for the auth to complete - otherwise you may not have the auth token before continuing.
FWIW - FB may have changed their rules in that app are not supposed to post to stream without letting a user modify the post text. A better route might be to go with your original code snip (waiting for auth if that's the issue?). You can put a link to your game in the post params and then put the score in the caption or description params (non-edit fields). You could then use the FB dialog and not let people change scores.
You can use Graph API.
[_facebook requestWithMethodName:#"stream.publish"
andParams:params
andHttpMethod:#"POST"
andDelegate:nil];

iphone : FBConnect Auto post to user wall

i want to auto post to a user wall from my iphone applications ( without that "publish" "skip" dialog box ). how i can do that ?
the following should be called in the class that implements FBSessionDelegate and FBRequestDelegate:
Facebook *_facebook = [[Facebook alloc] initWithAppId:kAppId];
NSArray *_permissions = [[NSArray arrayWithObjects:
#"read_stream", #"offline_access",nil] retain];
[_facebook authorize:_permissions delegate:self];
And that is the call for fb post (should be used in the same class):
NSString *message = #"This is the message I want to post";
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
message, #"message",
nil];
[_facebook requestWithMethodName:#"stream.publish"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
If you want to post a message on the wall of other user you should include "uid" parameter in your params dictionary. Please consult http://developers.facebook.com/docs/reference/rest/stream.publish/
P.S. There are all the necessary examples in the iPhone Facebook connect SDK sample code, so please do not afraid to investigate just a little bit ;)