Likes API

This article describes what you can do with the plugin likes API. You can read more about Likes API on the facebook developers portal.

Permissions

  • A user access token with user_likes permission is required to see all pages liked by that person.

Basically,  Likes API allows us to find out the pages linked by the user, or check if the user has liked a specific page.

 

To get the list of a current user likes, you can use:

if(_likes.ContainsKey(userId)) {
			foreach(KeyValuePair<string,  FB_LikeInfo>  pair in _likes[userId]) {
				result.Add(pair.Value);
			}
		}

 

Each like is represented  as FBLikeInfo class instance and contains the following info:

public string id;
public string name;
public string category;

 

You can also find out if the user has liked a specific page, for example, to find out if the current user has liked Union Assets Facebook Page we can use the following code:

bool IsLikes = SPFacebook.instance.IsUserLikesPage(SPFacebook.instance.UserId, UNION_ASSETS_PAGE_ID);

 

Of course, before we can get this info, we need to load user likes info. We can do this in a few ways.

Load likes list for current user: 

SPFacebook.instance.LoadCurrentUserLikes();

 

Load likes for the specific user:

SPFacebook.instance.LoadLikes(UserId);

 

Load likes for specificity user and page:

SPFacebook.instance.LoadLikes(SPFacebook.instance.UserId, UNION_ASSETS_PAGE_ID);

 

As soon as the request is ready, the

public static event Action<FB_Result> OnLikesListLoadedAction = delegate {}

Action will be fired, and we will be able to use the function described in the first article section.

The following code snippet shows how to retrieve the user's likes

SPFacebook.instance.LoadCurrentUserLikes();
SPFacebook.OnLikesListLoadedAction += OnLikesLoaded;


private void OnLikesLoaded(FB_APIResult result) {
	//The likes is loaded so now we can find out for sure if user is like our page
		bool IsLikes = SPFacebook.Instance.IsUserLikesPage(SPFacebook.instance.UserId, UNION_ASSETS_PAGE_ID);
		if(IsLikes) {
			SA_StatusBar.text ="Current user Likes union assets";
		} else {
			SA_StatusBar.text ="Current user does not like union assets";
		}
}

 

More examples can be found under the Facebook Example scene.