Facebook Like/Unlike gives error message - ios

I am building an application which implements Facebook. Through this application I can like and comment on a post. I am fetching Home feeds for the logged in user. Now for Like operation I am using the graph API :
https://graph.facebook.com/POST_ID/likes?&access_token=ACCESS_TOKEN
with HTTPMethod: POST
Here, the POST_ID is the ID I am getting for each feed and the access token of the logged in user.
Now in most of the cases I can like the feed using this API. But some posts don't have Like Connections. How do I know which feed has a like connection & which does not.
Now coming to Unlike. For Unlike operation I am using the graph API :
https://graph.facebook.com/POST_ID/likes?&access_token=ACCESS_TOKEN
with HTTPMethod: DELETE
I can unlike some posts or feeds using this API. But for some I get an error message. In that case i am using for example:
This is the
POST_ID = 12345_67890. When I get error message for this I am using the 67890 as the POST_ID in this case & I am getting success to unlike a post/feed.
Again in using this 67890 as POST_ID gives error in some cases & in that case I am using OBJECT_ID if exists in the feed I am receiving. And I am getting true response in some cases.
But in rest of the cases I can't find any solution for Like & Unlike of a feed/post in Facebook.
Waiting for positive reply.

if you are using correct post_id of feed than it will never produce any error while liking/disliking post on facebook by following code
if do you want to like than change HTTPMethod to POST otherwise set DELETE for dislike. there is no need of accesstoken to call this api.if you are using so it will not produce any error
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/likes",#"Post_id"] parameters:nil HTTPMethod:#"DELETE" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error)
{
NSLog(#"error: %#", error.localizedDescription);
}
else
{
NSLog(#"ok!! %#",result);
}
}];

Related

Parameter for like count with facebook api V2.3

I am using this line of code for getting count of likes on facebook post
NSString *postId = #"1234567890_1234567890";
NSMutableDictionary* photosParams = [NSMutableDictionary dictionaryWithObjectsAndKeys:postId,#"ObjectId",nil];
NSString * str = (NSString *) [[[FBSDKGraphRequest alloc] initWithGraphPath:#"/{object-id}/likes"
parameters:photosParams
HTTPMethod:#"GET"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if ([error.userInfo[FBSDKGraphRequestErrorGraphErrorCode] isEqual:#200]) {
NSLog(#"%#",[error localizedDescription]);
}
}];
but I am getting error of unsupported url, any one have Idea what parameter I use to get result. I am using updated version of facebook api
As
https://developers.facebook.com/docs/graph-api/reference/v2.3/object/likes
states, you need
The same permissions required to view the parent object are required to view likes on that object.
That means that the permission setting for Posts (https://developers.facebook.com/docs/graph-api/reference/v2.3/post) apply:
For page posts, public posts by the page are retrievable with any valid access token. Posts by people on the page, posts by people which mention the page, or targeted page posts (by language or geography, for example) may require a user or page token.
A user access token with read_stream or user_posts permission for any other posts
Are you sure you're adding an Access Token to your request?
You can request multiple posts at once like this:
/?ids={object_id1},{object_id2}&fields=id,created_time,likes.summary(true).limit(0)

Fetch Facebook Posts for Public Page using Facebook SDK

I'm trying to fetch Facebook posts for a public page using the Facebook SDK for iOS. I can't seem to figure out how to get and then use the access token I need to make this request. Let's say it's the New York Times page. This shouldn't require any permissions, so I should be able to use the App Access Token...but again, I can't figure out how to get & use that.
The code I am using looks like this:
[FBRequestConnection startWithGraphPath:#"/nytimes/posts" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"DATA: %#", [result data]);
NSLog(#"ERROR: %#", error);
}];
I keep receiving an error saying that an access token is required.
Please help!
It means it asking you a permission to access the public post in view did load method put one line code like (readPermissions = #[#"public_profile", #"email",#"user_friends",#"user_likes",#"user_groups"];)
, for more details see this https://developers.facebook.com/docs/facebook-login/permissions/v2.2.

Can FBRequestConnection startForCustomAudienceThirdPartyID be used on its own?

I want to get the app user ID for Facebook, as described here:
https://developers.facebook.com/docs/ads-for-apps/custom-audiences-for-mobile-apps/
The code snippet they give is:
[FBRequestConnection startForCustomAudienceThirdPartyID:nil
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSString *id = error ? nil : [result objectForKey:#"custom_audience_third_party_id"];
// Stash off this id, send it to your server, etc. Use later to construct
// a custom audience. A result of 'nil' means that either the user can't be
// identified, or they've limited ad tracking via iOS 6.
}
];
This always returns error 400 for me. Answers like this suggest that it needs to be used as part of a larger piece of code but there's no mention of this on the Facebook page.
Can I just use the snippet above, or do I need to implement something around it?
I got this from the FB developer page:
Discussion:
This method will thrown an exception if either <[FBSettings defaultAppID]> or <[FBSettings clientToken]> are nil. The appID won't be nil when the pList includes the appID, or if it's explicitly set. The clientToken needs to be set via <[FBSettings setClientToken:]>.
.....
Please check the conditions above first. Ass per documentation
startForCustomAudienceThirdPartyID: -> should actually point to the current FBSession
Right after the session is created do this: [FBSession setActiveSession:session];
Answered here: Facebook iOS SDK 3.1: "Error: HTTP status code: 400"

Facebook graph api and nested query

I am trying to get Friendlist details using Facebook iOS SDK using the below query:
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startWithGraphPath:#"me?fields=id,name,friendlists&fields=id,name,members&fields=pic,can_post,id,link,name,pic_crop,pic_large,pic_small,pic_square,profile_type,username,picture,list_type" completionHandler:^(FBRequestConnection *connection,id user, NSError *error) {
}];
I know the mistake is in using the '&' while constructing the query but I am not knowing how to nest the query (to get details like pic, can_post, etc).
Can someone help?
You should do this
me?fields=name,about,accounts.fields(about)
if you'd like to request 'user' (include name, about) and account connection (include 'about' field)
2nd connection you must user . (dot notation) instead of ?fields=
You should use + startForMyFriendsWithCompletionHandler:(FBRequestHandler)handler:
if (FBSession.activeSession.isOpen) {
[FBRequestConnection startForMyFriendsWithCompletionHandler:^(FBRequestConnection *connection,id user, NSError *error) {
}];
}
Note that it would get all the fields from your friends. If you want only a few ones, you can use startWithGraphPath:completion: with #"me/friends?fields=id,name,pic_square as graph path.

FB Graph API - Create event on behalf of page -> error 403

I have been trying to create an event on the behalf of my page using the Facebook Graph API for iOS.
However, I always fail with the error code 403. I have tried to create an event on behalf of me - and surprisingly that works. The permissions are set appropriately as stated here.
My code goes like:
NSMutableDictionary *postParams = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
#"Test",#"name",
#"2012-11-11", #"start_time", nil];
[FBRequestConnection
startWithGraphPath:#"12345/events"
parameters:postParams
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(#"Error: %#", result);
} else {
NSLog(#"Success: %#", result);
}
}];
How I said, if I set the ID/events to my user ID instead of my Page's ID, it works. Could I have set up something badly on my page? Do I need admin rights for that page?
I found something about access tokens to "Perform the following operations as a Page" here. Do I need that? If so, how do I do it?
Thanks to everyone in advance!
To authenticate as a Page (and from there, edit the events) see this document: https://developers.facebook.com/docs/authentication/pages/
In short, authenticate as user who is an an admin of the Page. Then:
https://graph.facebook.com/me/accounts?access_token=xxx
in order to retrieve the access token for the Page (and indeed for any other Pages the user administers).
Then use that access token to administer the events for the Page.

Resources