3

I'm trying to publish a facebook like from my android app to a given postId = 10154194181895153.

I have read many ways to do so.

But each of them returned an fb response error.

Can you please explain me the difference between A and B?

I have tried to move A to a asyncTask (C) but it didn't help as you can see.

fbLikeBtn.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        // fbLike_optionA();
                        // fbLike_optionB();
                        fbLike_optionC();

                    }

Option A

{Response:  responseCode: 200, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: android.os.NetworkOnMainThreadException}, isFromCache:false}

.

private void fbLike_optionA() {

    SharedPreferences prefs = mOffersListActivity.getSharedPreferences(
            PublicMacros.SP_NAME, Context.MODE_PRIVATE);

    String fbAccessToken = prefs
            .getString(PublicMacros.FB_ACCESS_TOKEN, "");

    Bundle params = new Bundle();
    params.putString("object", "http://samples.ogp.me/10154194181895153");
    params.putString("access_token", fbAccessToken);

    Request request = new Request(Session.getActiveSession(),
            "me/og.likes", params, HttpMethod.POST);
    Response response = request.executeAndWait();
    // handle the response
}

Option B

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 104, errorType: OAuthException, errorMessage: An access token is required to request this resource.}, isFromCache:false}

.

private void fbLike_optionB() {
                    Request.Callback callback = new Request.Callback() {
                        public void onCompleted(Response response) {

                            if (response != null) {
                                FacebookRequestError error = response
                                        .getError();

                                if (error != null) {
                                    // error
                                } else {
                                    // success
                                }
                            }
                        }
                    };

                    Request request = new Request(Session
                            .getActiveSession(), "10154194181895153/likes",
                            null, HttpMethod.POST, callback);

                    RequestAsyncTask task = new RequestAsyncTask(request);
                    task.execute();

                }

Option C

{Response:  responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: An active access token must be used to query information about the current user.}, isFromCache:false}

.

private void fbLike_optionC() {
                    new LikeFbPostAsyncTask(mCurrentlockImage)
                            .execute("10154194181895153");
                }



public class LikeFbPostAsyncTask extends AsyncTask<String, Void, Void> {
        ImageButton mLockImage;
        Response fbServerResponse;

        public LikeFbPostAsyncTask(ImageButton lockImage) {
            mLockImage = lockImage;
        }

        @Override
        protected void onPreExecute() {
            Log.i("LikeFbPostAsyncTask", "Starting web task...");
        }

        @Override
        protected Void doInBackground(String... fbPostId) {
            fbLike_optionA();
            return null;
        }

        @Override
        protected void onPostExecute(Void res) {
        }
    }

Option D

     Request likeRequest = new Request(Session.getActiveSession(),
     fbPostId[0] + "/likes", null, HttpMethod.POST,
     new Request.Callback() {

     // here is non-ui thread
     @Override
     public void onCompleted(final Response response) {
     Log.i(TAG, response.toString());
     fbServerResponse = response;

     }
     });
     Request.executeBatchAndWait(likeRequest);
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • A is an Android error. You're doing a network task on the main thread. B has something wrong with your session. Do a check to make sure the session is opened. – Wenger Jun 06 '14 at 17:17
  • I know a is an android error, that's why I have done C. but it doesn't work – Elad Benda2 Jun 07 '14 at 05:58

3 Answers3

2

Original post here.

Try to use this library:

dependencies {
    compile 'com.shamanland:facebook-like-button:0.1.8'
}

The simplest way to add like button:

<com.shamanland.facebook.likebutton.FacebookLikeButton
    style="@style/Widget.FacebookLikeButton"
    app:pageUrl="http://url.to.like/page.html"
    app:pageTitle="Title of page"
    app:pageText="Short description of page"
    app:pagePictureUrl="http://url.to.like/picture.jpg"
    />

This view will be drawn in your layout: enter image description here

After clicking on it you will see the dialog with official Facebook 'Like' plugin.

Read more details.

Oleksii K.
  • 5,359
  • 6
  • 44
  • 72
0

Using the newest Graph API:

Request request = new Request(Session.getActiveSession(), post_id + "/likes", null, HttpMethod.POST, new Request.Callback(){

    @Override
    public void onCompleted(Response response){
    //Request complete
    }
};
request.executeAsync();

This will have the user like the post with post_id. To unlike, just change the HttpMethod to HttpMethod.DELETE.

Wenger
  • 989
  • 2
  • 12
  • 35
  • What's wrong with D? Also, there is a slight difference. I'm not doing a request batch. Also, is option D being run on a background thread? – Wenger Jun 11 '14 at 14:42
  • If you do `executeAndWait()`, you don't need to specify a callback. You would have to be on a background thread to use `executeAndWait()` and that method actually returns a `Response` object. `Response response = request.executeAndWait()`. – Wenger Jun 11 '14 at 14:43
  • `{Response: responseCode: 403, graphObject: null, error: {HttpStatus: 403, errorCode: 200, errorType: OAuthException, errorMessage: (#200) Permissions error}, isFromCache:false}` – Elad Benda2 Jun 11 '14 at 14:53
  • If you create the `Request` before the `Session` is initialized, you might have to use `setSession(Session.getActiveSession())` on the `Request`. But that error looks like a permissions issue. You might not be able to post a like to that id. https://developers.facebook.com/docs/android/login-with-facebook/v2.0 – Wenger Jun 11 '14 at 14:56
  • `{Response: responseCode: 403, graphObject: null, error: {HttpStatus: 403, errorCode: 200, errorType: OAuthException, errorMessage: (#200) Permissions error}, isFromCache:false}` while my permissions are: `[publish_checkins, status_update, photo_upload, video_upload, email, create_note, share_item, publish_stream, publish_actions, user_birthday, user_location, user_likes, user_interests, user_friends, user_status, basic_info]` how can it be? – Elad Benda2 Jun 11 '14 at 14:59
  • It could be that post id isn't available for the user to send a like request to. For example, I can't like a photo that a friend of a friend uploaded and shared only with his/her friends. – Wenger Jun 11 '14 at 15:42
  • from this url: what is the post_id ? `https://www.facebook.com/photo.php?fbid=10154208667515153&set=a.10154208667405153.1073741857.685560152&type=1&theater` – Elad Benda2 Jun 11 '14 at 16:16
-1

First you need to get an access token for the user. Try reading this,

https://developers.facebook.com/docs/facebook-login/access-tokens/

The you can append the access token to your request url or whatever method you choose.

In case you want to go through the entire login flow, check this example :

https://developers.facebook.com/docs/reference/php/facebook-api/

  • but i have added valid access token: `params.putString("access_token", fbAccessToken);` – Elad Benda2 Jun 09 '14 at 08:09
  • Try adding a log to see if you have a valid access token? i.e. if it is null or empty. How are you getting the access token? Via Graph Explorer? Check this previous question : http://stackoverflow.com/questions/16330330/how-to-get-facebook-user-access-token-in-android – Rajarshi Goswami Jun 09 '14 at 08:11
  • access token is not null or empty. I get it using login button – Elad Benda2 Jun 09 '14 at 08:24
  • You shouldn't need to manually get an access token. It should be saved with the `Session` object you get from calling `Session.getActiveSession()`. – Wenger Jun 10 '14 at 17:21
  • I've found this link and Its simple with Graph_API. I tried it in my browser with my access-token which I got from Graph-explore like this https://graph.facebook.com/me/accounts?access_token=xxxxxxxx and it returned json with the pages details , running fine – Achin Aug 11 '14 at 11:04
  • but how should i use Graph_API in android , i mean is there any code to Hit that link ? – Achin Aug 11 '14 at 11:05