Android Facebook Like Button

81 Views Asked by At

Is it possible that I place a like button on my android app clicking which likes a post which is publicly available on facebook? I do not want any additional windows to open for the task except may be for first time authentication.

All questions asked so far are related to opening a new window.

1

There are 1 best solutions below

0
Muntaser Ahmed On BEST ANSWER

It is possible. You can perform a Like Action using Facebook's Open Graph API. In order to do so, you need to meet certain conditions:

  1. The viewer of the in-app content is a Facebook user who has Facebook-authed and granted the app publish_actions permission
  2. The in-app content has an Open Graph object page that is properly marked-up using Open Graph metatags
  3. The viewer has intentionally clicked on a custom in-app “like button” associated with the in-app content

Steps at a high-level:

You need to first get the user's active Session (com.facebook.Session).

Session session = Session.getActiveSession();

Then you need to ensure that the user has the publish_actions permission. If not, request them.

List<String> requestedPermissions = Arrays.asList("publish_actions");
List<String> currentPermissions = session.getPermissions();
if (!currentPermissions.containsAll(requestedPermissions)) {
    Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, requestedPermissions);
    session.requestNewPublishPermissions(newPermissionsRequest);
}

Finally, send your request to "me/og.likes". Your request should include your Session, a Bundle including the Graph object you want to like, the HTTP method, and a callback that will execute once you receive a response.

Bundle postParams = new Bundle();
postParams.putString("object", myGraphObject);
Request request = new Request(session, "me/og.likes", postParams, HttpMethod.POST, myCallback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();

You want to perform these steps in your button's OnClickListener.

Hope this helps!

Resources:

Like Action

Publishing Conditions