我正在尝试将facebook从我的Android应用程序发布到给定的postId = 10154194181895153
。
我已经阅读了许多方法。
但是每个人都返回了fb响应错误。
你能解释一下A和B之间的区别吗?
我试图将A移动到asyncTask(C)但是它没有帮助,你可以看到。
fbLikeBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// fbLike_optionA();
// fbLike_optionB();
fbLike_optionC();
}
选项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
}
选项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();
}
选项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) {
}
}
选项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);
答案 0 :(得分:2)
尝试使用此库:
dependencies {
compile 'com.shamanland:facebook-like-button:0.1.8'
}
添加类似按钮的最简单方法:
<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"
/>
此视图将在您的布局中绘制:
点击它后,您将看到官方Facebook&#39; Like&#39;插件。
阅读more details。
答案 1 :(得分:0)
使用最新的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();
这将使用户喜欢post_id
的帖子。不同的是,只需将HttpMethod
更改为HttpMethod.DELETE
。
答案 2 :(得分:-1)
首先,您需要为用户获取访问令牌。 试试这个,
https://developers.facebook.com/docs/facebook-login/access-tokens/
您可以将访问令牌附加到您的请求网址或您选择的任何方法。
如果您想要浏览整个登录流程,请查看以下示例:
https://developers.facebook.com/docs/reference/php/facebook-api/