I am trying (failing alot!) to use the Facebook iOS sdk. I want to publish a story about the user running without leaving the app. I am trying to use the Facebook built in object "course" and the built in action for a run.
I find the documentation very confusing, my code has become very tangled and I'm sure its the worst way possible of trying to implement this solution.
The error I'm getting with the following code is:
2014-04-01 23:10:13.238 Fitness_App[2313:60b] Encountered an error posting to Open Graph: Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x16ba5190 {com.facebook.sdk:HTTPStatusCode=500, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 1611072;
message = "The action you're trying to publish is invalid because it does not specify any reference objects. At least one of the following properties must be specified: course.";
type = Exception;
};
};
code = 500;
}, com.facebook.sdk:ErrorSessionKey=}
I have been struggling with this and could not get a solution!
-(void) publishStory
{
// instantiate a Facebook Open Graph object
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];
// specify that this Open Graph object will be posted to Facebook
object.provisionedForPost = YES;
// for og:title
object[#"title"] = #"running title";
// for og:type, this corresponds to the Namespace you've set for your app and the object type name
object[#"type"] = #"fitness.course";
// for og:description
object[#"description"] = #"running description";
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
// get the object ID for the Open Graph object that is now stored in the Object API
NSString *objectId = [result objectForKey:#"id"];
NSLog([NSString stringWithFormat:#"object id: %#", objectId]);
// Further code to post the OG story goes here
// create an Open Graph action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"fitness.course"];
[FBRequestConnection startForPostWithGraphPath:#"/me/fitness.runs" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog([NSString stringWithFormat:#"OG story posted, story id: %#", [result objectForKey:#"id"]]);
} else {
// An error occurred, we need to handle the error
NSLog(#"Encountered an error posting to Open Graph: %#", error.description);
}
}];
} else {
// An error occurred
NSLog(#"Error posting the Open Graph object to the Object API: %#", error);
}
}];
}
Two places where things went wrong.
object[#"type"] = #"fitness.course";
The type should equal #"namespace:object".
[action setObject:objectId forKey:#"fitness.course"];
The key is your object name.
Check your code again and have fun ^-^
Try replacing your this sentence
"[action setObject:objectId forKey:#"fitness.course"];"
with this one
"[action setObject:objectId forKey:#"course"];"
Related
I am trying to retrieve list of FB friends which use the app. It was working fine until last month. I just realized that it is not working any longer. I am using the following block to achieve the results:
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
}
else
{
NSLog(#"%#",error)
}
}];
But quite contrary to previous results, Now I get the following error:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x174278280 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ErrorSessionKey=<PFReceptionist: 0x174026dc0>, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Unknown fields: username.";
type = OAuthException;
};
};
code = 400;
}}
Has the facebook sdk changed lately? It might be so because I can't get the gender and date of birth too now, which I was able to retrieve before. What do you reckon is the issue? How can I retrieve the friends who are using my app, as it is quite crucial for my app?
I fixed it using this block instead.
FBRequest *friendsRequest = [FBRequest requestForGraphPath:#"/me/friends"];
[friendsRequest startWithCompletionHandler: ^(FBRequestConnection *connection,NSDictionary* result,NSError *error) {
if(error)
{
NSLog(#"%#", error);
}
else
{
NSLog(#"%#", result);
}
}];
I'm using Parse Version 1.2.19 and I can't access a facebook user's name.
if ([PFFacebookUtils isLinkedWithUser:[PFUser currentUser]]) {
// Create Facebook Request for user's details
FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSString *displayName = result[#"name"];
if (displayName) {
self.title =[NSString stringWithFormat:NSLocalizedString(#"Welcome %#!", nil), displayName];
}
}else{
NSLog(#"%#",[error description]);
}
}];
}
I get the following error:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x29e24040 {com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}
These 2 posts claim the issue is fixed in the latest parse version:
https://www.parse.com/questions/error-when-trying-to-reauthorise-facebook-user
https://www.parse.com/questions/oauthexception-code-2500-an-active-access-token-must-be-used
It seems I needed this:
[PFFacebookUtils initializeFacebook];
Before calling the request. That line of code wasn't being called when the app just reloaded.
I am implementing the Checkins Facebook Graph API using Facebook SDK. This is the code for Checkins
NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:accsstoken,#"access_token",#"253651184683030",#"place",#"I m here in this place",#"message",#"30.893075018178,75.821777459326",#"coordinates", nil];
[FBRequestConnection startWithGraphPath:#"/me/checkins"
parameters:dict
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Error...%#",error);
}];
When I tried this above code. It gives me following error:
error = {
code = 160;
message = "(#160) Invalid coordinates. Coordinates must contain at least latitude and longitude.";
type = OAuthException;
};
It gives the coordinates issue. Is there a different way to pass the coordinates parameters? Please help me out of this issue.
As far as I know checkins are deprecated and you should use post with place parameter.
And here is the link. Facebook SDK reference
Edit: For people who too lazy to check the link, there is the sample code from Facebook.
// Create an object
NSMutableDictionary<FBOpenGraphObject> *restaurant = [FBGraphObject openGraphObjectForPost];
// specify that this Open Graph object will be posted to Facebook
restaurant.provisionedForPost = YES;
// Add the standard object properties
restaurant[#"og"] = #{ #"title":#"Restaurant Name", #"type":#"restaurant.restaurant", #"description":#"a description", #"image":image };
// Add the properties restaurant inherits from place
restaurant[#"place"] = #{ #"location" : #{ #"longitude": #"-58.381667", #"latitude":#"-34.603333"} };
// Add the properties particular to the type restaurant.restaurant
restaurant[#"restaurant"] = #{#"category": #[#"Mexican"],
#"contact_info": #{#"street_address": #"123 Some st",
#"locality": #"Menlo Park",
#"region": #"CA",
#"phone_number": #"555-555-555",
#"website": #"http://www.example.com"}};
// Make the Graph API request to post the object
FBRequest *request = [FBRequest requestForPostWithGraphPath:#"me/objects/restaurant.restaurant"
graphObject:#{#"object":restaurant}];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Sucess! Include your code to handle the results here
NSLog(#"result: %#", result);
_objectID = [result objectForKey:#"id"];
alertTitle = #"Object successfully created";
alertText = [NSString stringWithFormat:#"An object with id %# has been created", _objectID];
[[[UIAlertView alloc] initWithTitle:alertTitle
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
}
}];
Checkins have been deprecated in favor of attaching place information to posts, or tagging places in Open Graph stories.
You can refer here
https://developers.facebook.com/docs/graph-api/reference/user/checkins/
I'm developing a simple app that has a feature to compose user pictures and post them as a OG Story to facebook.
I followed the Facebook docs and I'm issuing the POST requests (I know I could batch them, I'm just trying to get any version working), I do get an story ID, but I can't find the story on Facebook. (permission set to friends)
I'm posting it with the path /me/:
When I check in the Graph Explorer this path returns an empty { 'data' : [] }
I know the story won't be posted on my profile, but I need to see it somewhere.
I need to test things such as deep linking (I know url is currently set to nil), but I can't find the story! The only place I could manage to see the picture is when I go to
developer page and click on 'preview'.
Any ideas?
Thanks in advance.
Here is the source code:
- (void) publishPhoto
{
// stage an image
[FBRequestConnection startForUploadStagingResourceWithImage:self.photo.image completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog(#"Successfuly staged image with staged URI: %#", [result objectForKey:#"uri"]);
// instantiate a Facebook Open Graph object
NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPostWithType:#"<APPNAMESPACE>:picture" title:#"" image:#[#{#"url":[result objectForKey:#"uri"], #"user_generated" : #"true"}] url:nil description:#""];
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
// get the object ID for the Open Graph object that is now stored in the Object API
NSString *objectId = [result objectForKey:#"id"];
NSLog([NSString stringWithFormat:#"object id: %#", objectId]);
// create an Open Graph action
id<FBOpenGraphAction> action = (id<FBOpenGraphAction>)[FBGraphObject graphObject];
[action setObject:objectId forKey:#"picture"];
// create action referencing user owned object
[FBRequestConnection startForPostWithGraphPath:#"/me/<APPNAMESPACE>:take" graphObject:action completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(!error) {
NSLog([NSString stringWithFormat:#"OG story posted, story id: %#", [result objectForKey:#"id"]]);
[[[UIAlertView alloc] initWithTitle:#"OG story posted"
message:#"Check your Facebook profile or activity log to see the story."
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Encountered an error posting to Open Graph: %#", error.description);
}
}];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Encountered an error posting to Open Graph: %#", error.description);
}
}];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error staging an image: %#", error.description);
}
}];
}
Try it with iPhone device instead of simulator. It'll work
i am trying to create an event but getting error . i am writing following code to create event using facebook sdk 3.1
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"My Test Event",#"name",
#"Bangalore",#"location",
#"1297641600",#"start_time",
#"1297468800",#"end_time", nil];
NSString * pRequest = [NSString stringWithFormat:#"me/events"];
[FBRequestConnection startWithGraphPath:pRequest parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(#"Facebook Post Success..");
} else
{
NSLog(#"Facebook Post Failed..");
NSLog(#"ERROR : %#", error.localizedDescription);
}
}];
error :
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0xa180200 {com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Invalid parameter";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}
can anybody help .....
thanks in advance
The error is a 400 error a bad request. This means there is something wrong with you're request. Also the error that Facebook returns suggest that you have an invalid parameter. So I suggest to double check you're parameters that you are sending and make sure you sending the correct amount of parameters in the right order and the right values.