Liking a facebook status using og_likes iOS functional error - ios

I am trying to implement "Liking a Status" into my application. I am using the Facebook SDK references and have come up with this bit of code :
- (void) likeAStatus : (NSString *) postId {
NSString *post = [NSString stringWithFormat:#"http://facebook.com/%#", postId];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
post, #"object",
nil
];
/* make the API call */
[FBRequestConnection startWithGraphPath:#"/me/og.likes"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result is %#", result);
NSLog(#"Error is %#", error);
NSString *idFromCreate = [NSString stringWithFormat:#"/%#", [result objectForKey:#"id"]];
/* make the API call */
[FBRequestConnection startWithGraphPath:idFromCreate
parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result is %#", result);
NSLog(#"Error is %#", error);
}];
}];
}
Now when I run this on a post that I haven't liked before (I am simply had coding the postID for now) I get this response in the console : (I was just printing the action ID to make sure it was parsing the response properly)
2014-01-13 15:59:43.766 Unifeed[5336:70b] Result is {
id = 10151765515511916;
}
2014-01-13 15:59:43.767 Unifeed[5336:70b] Error is (null)
2014-01-13 15:59:43.767 Unifeed[5336:70b] The ID is /10151765515511916
2014-01-13 15:59:44.052 Unifeed[5336:70b] Result is {
"FACEBOOK_NON_JSON_RESULT" = true;
}
2014-01-13 15:59:44.053 Unifeed[5336:70b] Error is (null)
This is looking like it works, unfortunately when I go and check my Facebook and look at this specific post it is not liked.... If I check my activity log on Facebook I do not see that I liked this status.
Any ideas as to what is going on or any suggestions to a different method of liking a status update? Also is the "FACEBOOK_NON_JSON_RESULT" = true a good thing?
Thanks much!

I have figured it out - it was a differentiation of the graph API call. Now the code looks like this :
- (void) likeAStatus : (NSString *) postId {
NSString *post = [NSString stringWithFormat:#"%#/likes", postId];
/* make the API call */
[FBRequestConnection startWithGraphPath: post
parameters:nil
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSLog(#"Result is %#", result);
NSLog(#"Error is %#", error);
}];
}
Hope this helps someone out in the future!!
Andy

Related

formatting a url to appear as string in a facebook post

I am trying to post a link formatted as a string on facebook. Please see the relevant code below:
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
NSString *modifiedTitle = [NSString stringWithFormat:#"<a href= https://www.google.com> My App </a>"];
[params setObject:modifiedTitle forKey:#"message"];
[params setObject:UIImagePNGRepresentation(picture) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
if (error)
{
//showing an alert for failure
NSLog(#"ERROR in posting image to Facebook");
}
else
{
//showing an alert for success
NSLog(#"Result after posting image successfully = %#",result);
}
// button.enabled = YES;
}];
The message appeared as:
<a href= https://www.google.com> My App </a>
instead of
My App
For one thing, ensure you're posting an immmutable dictionary into the startWithGraph... method by using the copy method:
NSDictionary* paramsImmutable = params.copy;
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:paramsImmutable
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
That way you won't have any changes to worry about on that object during the method's lifetime.
Next, is your params including any sort of quotes around the url? I.e.
NSString *modifiedTitle = #"<a href='https://www.google.com'>My App</a>";
That's just a start.

IOS Facebook SDK, fetch user info

is it possible, using the Facebook SDK, to get the user info from his user id? An important note is that the user would have approved the application and its permissions before the app make the request.
If it is possible, what is the right way to achieve this? I am trying using this code:
[FBRequestConnection startWithGraphPath:#"/100007046299250"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
}];
But I get the following error message:
Error for request to endpoint '/100007046299250': An open FBSession must be specified for calls to this endpoint.
Thanks
After login authentication, you can get details from active FBSession like below
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *user,
NSError *error) {
if (!error) {
NSString *firstName = user.first_name;
NSString *lastName = user.last_name;
NSString *facebookId = user.id;
NSString *email = [user objectForKey:#"email"];
NSString *imageUrl = [[NSString alloc] initWithFormat: #"http://graph.facebook.com/%#/picture?type=large", facebookId];
}
}];
}

Fetch user interests and likes from Facebook along with images using Facebook iOS SDK

I want to fetch Facebook logged in user's interests and likes, which I'm able to do.
But I'm not getting the image for pages liked by user or image for user interests.
The following code lets me fetch user likes:
[FBRequestConnection startWithGraphPath:#"/me/likes"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
/* handle the result */
NSLog(#"User interests data: %#",result);
}];
which returns the following json data:
data = (
{
category = "Musician/band";
"created_time" = "2014-08-08T05:09:56+0000";
id = 9770929278;
name = Adele;
},
{
category = "Musician/band";
"created_time" = "2014-08-08T05:02:09+0000";
id = 5027904559;
name = Shakira;
}
);
but, I want images for the pages liked. How can I do that?
Any help would be appreciated.
Thanks in advance.
You can execute 1 more Graph API call for each for the likes:
[FBRequestConnection startWithGraphPath:#"/me/likes"
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
NSArray *data = [result objectForKey:#"data"];
for(NSDictionary *liked in data) {
NSString *page_id = [liked objectForKey:#"id"];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"/%#/picture", page_id] parameters:nil HTTPMethod:#"GET" completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
) {
// Get your profile pic here
}
}];
}
}];
It will return the public details of Page. See documentation for more information.

How to get large picture size from Facebook graph API for news feed

I am stuck with getting picture from Facebook Graph API for news feed with small size. This is the url I get https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t1.0-0/10363492_10202184985725870_7374705736674502849_s.jpg. Not sure how will get the picture with size o.jpg
Finally, figured it out. Here is what I did.
NSString *query = [NSString stringWithFormat:#"SELECT pid, object_id, src_big, src_big_width, src_big_height FROM photo WHERE object_id = %#", wallPost[#"object_id"]]; //begins from SELECT........
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
query, #"q",
nil];
[FBRequestConnection startWithGraphPath:#"/fql" parameters:params HTTPMethod:#"GET" completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
//do your stuff
if (!error) {
NSArray *photo = [result objectForKey:#"data"];
NSString *photoString = [[photo valueForKey:#"src_big"]componentsJoinedByString:#""];
//NSLog(#"result %# %#", photoString);
}
}];

iOS Facebook Api, make a like to a comment post

I want to like a comment of a post on Facebook, I use the same as like the post. For like a post, it works, but for like a comment fail.
Doc:
https://developers.facebook.com/docs/graph-api/reference/object/likes
My Code:
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"/%#/likes", postId_]
parameters:nil
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{ //Error:
}];
The Error is:
Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x158999b0 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 100;
message = "(#100) Error finding the requested story";
type = OAuthException;
};
};
code = 400;
}, com.facebook.sdk:ErrorSessionKey=}
Here is how I do it, and it works like a charm
// post is my module object, encapsulates the info form the post
// pass the post ID
NSString *graphPath = [NSString stringWithFormat:#"%#/likes", post.postID];
FBRequest *request = [FBRequest requestForGraphPath:graphPath];
// DELETE or POST the like
NSString *method = post.liked?#"DELETE":#"POST";
[request setHTTPMethod:method];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
BOOL success = YES;
success = (error)?NO:YES;
if(success) {
}
}];
Note: make sure you have publish permissions
In my project I use the following code:
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:URL, #"object", nil];
if (FBSession.activeSession.isOpen) {
if (FBSession.activeSession.accessTokenData.accessToken) {
[FBRequestConnection startWithGraphPath:#"/me/og.likes"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"Just liked on Facebook!");
}];
} else NSLog(#"Cannot open FBSession");
}
There no code for opening or initializing FBSession - I hope, it's not a problem?
I think, we all can trust Facebook, yes? :)
Go to https://developers.facebook.com/docs/reference/opengraph/action-type/og.likes, select "IOS SDK" tab and look at the code.

Resources