Search Unity

Facebook SDK (6.22) - Using Open Graph to Like and check if a page was liked.

Discussion in 'Scripting' started by AlanGameDev, Jan 20, 2016.

  1. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Hello there.

    I'm trying to add a 'like' button to an Android/iOS game, and it seems that isn't supported directly by the Facebook SDK for Unity, so you have to use Open Graph. The documentation is really lacking (at least I couldn't find any), so I'm practically coding blindingly, using common sense and assumptions for pretty much everything.

    In this case, I want the user to 'like' a given page, I'm using the sample page as this link shows:
    https://developers.facebook.com/docs/reference/opengraph/action-type/og.likes

    OK, first problem, I don't know how to use the docs from the page above in the Facebook SDK for Unity, but since there's an example in this page on how to call Open Graph API:
    https://developers.facebook.com/docs/unity/reference/current/FB.API

    I've assumed I could use the cURL docs to get an example on how to use it as a parameter in the FB.API:
    Code (text):
    1. curl -X POST \
    2.      -d "object=http%3A%2F%2Fsamples.ogp.me%2F226075010839791" \
    3.         https://graph.facebook.com/v2.5/me/og.likes
    So, that would be this string, following the logic of url?par1&par2&par3
    Code (CSharp):
    1. "https://graph.facebook.com/v2.5/me/og.likes?object=http://samples.ogp.me/226075010839791"
    But that didn't work, so I discovered you have to pass the token and user ID as in this page:
    https://developers.facebook.com/docs/opengraph/guides/og.likes#publishing

    So, here is the code now:
    Code (CSharp):
    1. FB.API(
    2. "https://graph.facebook.com/"+FB.UserId+"/og.likes?object=http://samples.ogp.me/226075010839791&access_token="+FB.AccessToken,
    3. Facebook.HttpMethod.POST, LikeCallback, FBData
    4. );
    This time it seemed to work, according to the docs it returns a page ID if it was successful, here is the resulting FBResult.Text:
    Code (text):
    1. {"id":"835163749939499","url":"https:\/\/graph.facebook.com\/479409335579524\/og.likes"}
    FBResult.Error was 0

    The app (although it's a testing app), has the publish_actions permission, and I can see it on my Facebook as allowed. However, running that code didn't yield any result whatsoever, the page wasn't liked on Facebook.

    Besides a button to like the page, I also need to check if the page was liked by a given user, in this case I've read somewhere that, besides the user_likes permission, the string would be very simple: "me/likes/PAGEID". And I'm assuming it would return some kinda true/false in the string FBResult.Text.

    Also, after adding the 'like' button, I want to add a button for the user to rate the game on Facebook, and it seems you also need to use Open Graph for that. But I totally couldn't find any docs regarding that, in fact the only mention I could find is in the bottom of the page linked above:
    But I've found absolutely nothing on that 'rated action'.

    It astonishes me that something so widely used is so lacking in something as important as docs. The docs are also overly verbose, I've read a lot of mumbo-jumbo only to realize all of that was useless. Docs should be straight to the point.

    Any help is appreciated.
     
  2. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Actually, it worked now. I guess it was some problem on Facebook's end, perhaps some server propagation delay, don't know. I didn't change a thing and it worked fine. However, I just noticed that you can't use that to like pages :(.