Skip to content Skip to sidebar Skip to footer

Facebook Graph Api Unlike A Liked Post

I have a method in my app that allows the user to 'like' a post in his/her news feed. It's done with a simple graph request using HttpMethod.POST. But when I try to do an 'unlike'

Solution 1:

When you grab the post id from the graph data, it should be in a format like: XXXXX_YYYYY. The XXXXX is simply the users id and YYYYY is the actual post id. What you need to do is extract and use just the YYYYY portion of the post id that graph gives you. so instead of graph.facebook.com/XXXXX_YYYYY/likes.... you want to send graph.facebook.com/YYYYY/likes. This will work with both liking and unliking, you can test in graph explorer first before hacking together a substring extraction method.

Not sure how to extract a section of a string on Android, but I know in Objective-C/iOS, it can be done like this (code not tested, for reference/idea) :

SString *actualPostIdStr;      //The String we will put the actual postId inNSString *oldIdStr          =   //<the string in format XXXXX_YYYYY>NSInteger charCount         =   [oldIdStr length]; //get the length of the original XXXXX_YYYYY stringNSRange fRangeCount         =   [oldIdStr rangeOfString:@"_"]; //get count of characters to remove (XXXXXX)if (fRangeCount.location != NSNotFound){
    NSInteger startingPos   =   fRangeCount.location + 1; //get the starting character position of the actual postId
    actualPostIdStr         =   [[oldIdStr substringWithRange:NSMakeRange(startingPos, charCount - startingPos)] copy];
}

Hope this helps.

EDIT: Ok, so I have been playing around with likes all day... It seems that this method sometimes doesn't work, but it all depends on the type of graph object which you are attempting to like/unlike. For example... plain status posts, this method works perfectly. However, I ran into a problem when trying to like a photo with a message/story post object. It turns out, in the graph data for this type of post object with photo, there is an additional paramter called "object_id", in addition to the plain "id" that is in status post graph data. in this case, with the photo and story post, you need to pass the "object_id", unaltered for successful unliking.

This mess seems like either a bug on FB's end, or they are making changes and disallowing likes/unlikes from graph api & just forgot/haven't to told us yet :) hopefully the former. In the mean time, you're just going to have to use my above answer, but just make sure you test with as many different types of post objects you can find, and use if conditions when a different id (portion of "id"... "object_id"... etc) is required.

Solution 2:

Sometimes Daniel McCarthy's method don't work. In this case we need get Graph API request XXX_YYYY, find field object_id and unlike this object_id fid.

Post a Comment for "Facebook Graph Api Unlike A Liked Post"