简体   繁体   中英

Android and Facebook Like action with Open Graph object

Facebook like action has been released for mobile apps.

As the documentation says :

To publish a built-in Like action on an Open Graph object,
invoke the following HTTP POST request with a user’s access
token and the url of the Open Graph object.
This Open Graph object can be of any type.

curl -X POST \
    -F 'access_token=USER_ACCESS_TOKEN' \
    -F 'object=OG_OBJECT_URL' \
    https://graph.facebook.com/[User FB ID]/og.likes

Usually, to make a request eg for user info, I use this kind of code:

Facebook facebook;
facebook = new Facebook(AppConfig.FACEBOOK_APP_ID);

if (facebook.isSessionValid())
{
    JSONObject obj = facebook.request("me");
    ...
}
else
{
    facebook.authorize(...)
    {
        @Override public void onComplete(Bundle values)
        {
            String token = facebook.getAccessToken();
            long expires = facebook.getAccessExpires();
            ...
        }
    }
}

My question is dead simple, yet I can't find the answer, how do I make the "graph.facebook.com/[User FB ID]/og.likes" request, in JAVA code, once I retrieved user access token and expires ?

You would do something like the following:

1/ After you've initialized the facebook instance you can declare the following:

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);

2/ Then when you want to post the like you can use the following code:

Bundle params = new Bundle();
params.putString("object",OG_OBJECT_URL);

mAsyncRunner.request("me/og.likes", params, 
                    "POST", new BaseRequestListener() {
                       @Override
                       public void onComplete(final String response, final Object state) {
                           // handle success
                       }
                    });

3/ You can pick off the implementation for BaseRequestListener here: https://github.com/fbsamples/android-social-cafe/blob/master/src/com/facebook/samples/socialcafe/BaseRequestListener.java

Finally to see a sample of Open Graph like in action see this sample: https://github.com/fbsamples/android-social-cafe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM