问题
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);
回答1:
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:

After clicking on it you will see the dialog with official Facebook 'Like' plugin.
Read more details.
回答2:
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
.
回答3:
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/
来源:https://stackoverflow.com/questions/24087245/fail-to-publish-a-facebook-like-from-my-android-app-to-a-given-postid