7

I'm trying to implement Like via the facebook open-graph-api with the Facebook iOS SDK 3.0. Everything seems to work except the FbGraphObject and that's because I have no idea how it should look because this clearly does not work.

What I'm trying to do is to like a url posted as an object. A simple Like with via the open-graph.

The error message I get the the code below is:

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: object.

The code I use is this:

    FBGraphObject *objectToLike = [[FBGraphObject alloc]initWithContentsOfURL:[NSURL URLWithString:facebookLike.titleLabel.text]];

    FBRequest *requestLike = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession] graphPath:@"me/og.likes" graphObject:objectToLike];

    FBRequestConnection *connection = [[FBRequestConnection alloc] init];
    [connection addRequest:requestLike
         completionHandler:
     ^(FBRequestConnection *connection, id result, NSError *error) {
         if (!error &&
             result) {

             DLog(@"NothingWentWrong");
         }

         DLog(@"MajorError: %@", error);

     }
     ];

    [connection start];

UPDATE:

Checked some more info and my guess it to use this method: https://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBGraphObject/#//api/name/graphObject

To somehow create an object. It's the graphObject method that I probably need to do something with. Any help at all would be appreciated.

Joakim Engstrom
  • 6,243
  • 12
  • 48
  • 67

4 Answers4

6

I've actually manage to create a simple and quite dirty solution of this. The solution does not seem optimal but it's currently a working solution.

If anybody has used the explorer tool on facebook on this url: https://developers.facebook.com/tools/explorer/

You know how the URL will look like when facebook is sharing a like. It has to have the URL and an access-token. So my solution became just to disregard sending anything from the Facebook SDK and just send a post request to the same URL that I've used in the explorer tool.

There seems to be some referencing to it on the facebooks docs if you look closely and deep, but no one explains exactly how to actually make the connection, so this is my solution:

NSString *urlToLikeFor = facebookLike.titleLabel.text;

NSString *theWholeUrl = [NSString stringWithFormat:@"https://graph.facebook.com/me/og.likes?object=%@&access_token=%@", urlToLikeFor, FBSession.activeSession.accessToken];
NSLog(@"TheWholeUrl: %@", theWholeUrl);

NSURL *facebookUrl = [NSURL URLWithString:theWholeUrl];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:facebookUrl];
[req setHTTPMethod:@"POST"];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];

NSLog(@"responseData: %@", content);

If you look at the code I just take the url and puts two dynamic strings in the url, one with the object-url and one with the access token. I create a URLRequest and make it a POST request, and the response from facebook gets logged so one actually can see if the like go through or not.

There might be some performance improvements that can be done with the actual requests but I will leave it up to you if you see any slowdowns.

I'm still interested in other solutions but this is the one I will use for now.

Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
Joakim Engstrom
  • 6,243
  • 12
  • 48
  • 67
  • Can you please tell me what is "urlToLikeFor", is it only 'id' of or complete url of any page – Surender Rathore Oct 10 '12 at 12:33
  • Complete URL of the web-page to like, this web-page has to implement the meta-tags that facebook provides. – Joakim Engstrom Oct 10 '12 at 13:33
  • Thanx For reply... you mean like this http://stackoverflow.com/questions/9038025/facebook-object-debugger-property-ogurl-could-not-be-parsed-as-type-url – Surender Rathore Oct 10 '12 at 15:10
  • Yep... Like that. It is important to double-check the values. And create an object on developer.facebook.com so you actually can like the web-page. – Joakim Engstrom Oct 10 '12 at 15:27
  • The object you create in the open graph section under your app in the developer page on facebook. There you can get the meta-tags when you created the object. You have to get the object approved to before anyone else can use it, like this: http://developers.facebook.com/docs/opengraph/objects/ – Joakim Engstrom Oct 10 '12 at 15:53
  • This doesn't seem to work for me. Is this still a valid way to accomplish this? – Kyle Clegg Jul 16 '13 at 16:15
  • I don't know, was a while I worked with the code. I think Facebook change something in with their likes but not sure this affects it. The best way is to actually test with the explorer tool, if that does not work some more investigating is valid. – Joakim Engstrom Jul 17 '13 at 07:21
  • 1
    What is facebookLike.titleLabel.text? it should be "http://www.facebook.com/samplepage". or some other? – user40910 Aug 11 '14 at 07:29
2

We don't currently support Like through our Graph API. What you can look through is something like this : https://developers.facebook.com/docs/opengraph/actions/builtin/likes/

0

I’m not sure what initWithContentsOfURL does, but from the name I guess it tries to actually load content from a given URL(?).

You only have to give the URL as a text parameter – a URL is what represents an Open Graph object. Facebook will do the rest, scraping the page behind that URL and reading it’s OG meta tags, etc.

CBroe
  • 91,630
  • 14
  • 92
  • 150
  • by url as a text parameter do you mean a string? and can you give me some pointers on how to do it? – Joakim Engstrom Aug 27 '12 at 09:04
  • Yes, and no (since I don’t do any iPhone dev stuff). – CBroe Aug 27 '12 at 09:07
  • The only problem is that the instance of FBGraphObject does not seem to accept a string in any way. The problem here is that the FBGraphObject is somehow wrong and I can't make my string an object. I'm pretty sure I've implemented the FBGraphObject wrong. – Joakim Engstrom Aug 27 '12 at 09:14
0

Maybe just this?

    FBRequest *requestLike = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession]
    graphPath:@"me/og.likes"
    graphObject:[NSURL URLWithString:facebookLike.titleLabel.text]];
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Jack Nutting
  • 606
  • 3
  • 11
  • 1
    tried it, I somehow have to implement the protocol FBOpenGraphAction, But I'm still not quite sure how. http://developers.facebook.com/docs/sdk-reference/iossdk/3.0/protocol/FBOpenGraphAction/ – Joakim Engstrom Aug 29 '12 at 11:50