我正在尝试使用sdk版本3.14.1从Facebook更新Facebook上的状态。
我可以在Facebook上通过帐户在developers.facebook中创建应用程序的同一帐户更新状态。
但无法从不同的帐户发帖。我尝试了很多,但无法得到解决方案。 请给我一个适当的解决方案或参考链接。 提前谢谢。
答案 0 :(得分:3)
嗨@Kamlesh Arya我的代码在这里
package com.mihir.fbdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.fbdemo.R;
import com.facebook.FacebookAuthorizationException;
import com.facebook.FacebookOperationCanceledException;
import com.facebook.FacebookRequestError;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.model.GraphObject;
import com.facebook.model.GraphUser;
import com.facebook.widget.FacebookDialog;
public class Main extends Activity implements OnClickListener
{
private Button btn_login = null, btn_post = null;
private TextView tv_welcome = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_login = (Button) findViewById(R.id.btn_login);
btn_post = (Button) findViewById(R.id.btn_post);
tv_welcome = (TextView) findViewById(R.id.tv_welcome);
btn_login.setOnClickListener(this);
btn_post.setOnClickListener(this);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
// Can we present the share dialog for regular links?
canPresentShareDialog = FacebookDialog.canPresentShareDialog(this,
FacebookDialog.ShareDialogFeature.SHARE_DIALOG);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btn_login:
loginSession();
break;
case R.id.btn_post:
onClickPostStatusUpdate();
break;
}
}
private void loginSession()
{
// start Face-book Login
Session.openActiveSession(Main.this, true, new Session.StatusCallback()
{
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception)
{
if (session.isOpened())
{
// make request to the /me API
Request.newMeRequest(session, new Request.GraphUserCallback()
{
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response)
{
if (user != null)
{
tv_welcome.setText("Hello " + user.getName() + "!");
}
}
}).executeAsync();
}
}
});
}
private boolean canPresentShareDialog;
private PendingAction pendingAction = PendingAction.POST_STATUS_UPDATE;
private static final String PERMISSION = "publish_actions";
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (pendingAction != PendingAction.NONE &&
(exception instanceof FacebookOperationCanceledException ||
exception instanceof FacebookAuthorizationException)) {
new AlertDialog.Builder(Main.this)
.setTitle("Cancel")
.setMessage("permission_not_granted")
.setPositiveButton("ok", null)
.show();
pendingAction = PendingAction.NONE;
} else if (state == SessionState.OPENED_TOKEN_UPDATED) {
handlePendingAction();
}
// updateUI();
}
private void onClickPostStatusUpdate() {
performPublish(PendingAction.POST_STATUS_UPDATE, canPresentShareDialog);
}
private void performPublish(PendingAction action, boolean allowNoSession) {
Session session = Session.getActiveSession();
Log.e("performPublish", "performPublish");
if (session != null) {
pendingAction = action;
if (hasPublishPermission()) {
// We can do the action right away.
handlePendingAction();
return;
} else if (session.isOpened()) {
// We need to get new permissions, then complete the action when we get called back.
session.requestNewPublishPermissions(new Session.NewPermissionsRequest(this, PERMISSION));
return;
}
}
if (allowNoSession) {
pendingAction = action;
handlePendingAction();
}
}
private enum PendingAction {
NONE,
POST_PHOTO,
POST_STATUS_UPDATE
}
private boolean hasPublishPermission() {
Session session = Session.getActiveSession();
return session != null && session.getPermissions().contains("publish_actions");
}
@SuppressWarnings("incomplete-switch")
private void handlePendingAction() {
Log.e("handlePendingAction", "handlePendingAction");
PendingAction previouslyPendingAction = pendingAction;
// These actions may re-set pendingAction if they are still pending, but we assume they
// will succeed.
pendingAction = PendingAction.POST_STATUS_UPDATE;
switch (previouslyPendingAction) {
case POST_STATUS_UPDATE:
postStatusUpdate();
break;
}
}
private void postStatusUpdate() {
Log.e("postStatusUpdate", "postStatusUpdate");
if (canPresentShareDialog) {
FacebookDialog shareDialog = createShareDialogBuilderForLink().build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else if (hasPublishPermission()) {
final String message = "post from other account";
Request request = Request
.newStatusUpdateRequest(Session.getActiveSession(), message, null, null, new Request.Callback() {
@Override
public void onCompleted(Response response) {
showPublishResult(message, response.getGraphObject(), response.getError());
}
});
request.executeAsync();
} else {
pendingAction = PendingAction.POST_STATUS_UPDATE;
}
}
private void showPublishResult(String message, GraphObject result, FacebookRequestError error) {
String title = null;
String alertMessage = null;
if (error == null) {
title = "success";
// String id = result.cast(GraphObjectWithId.class).getId();
alertMessage = "successfully_posted_post";
} else {
title = "error";
alertMessage = error.getErrorMessage();
}
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(alertMessage)
.setPositiveButton("ok", null)
.show();
}
private FacebookDialog.ShareDialogBuilder createShareDialogBuilderForLink() {
return new FacebookDialog.ShareDialogBuilder(this)
.setName("Hello Facebook")
.setDescription("The 'Hello Facebook' sample application showcases simple Facebook integration")
.setLink("http://developers.facebook.com/android");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
}
答案 1 :(得分:0)
简单,
您需要Disable testing mode on facebook developer console
然后您才能从其他帐户更新Facebook上的状态。
分析Facebook开发者控制台,您可以在应用程序设置菜单中选择。 然后在没有测试模式的情况下设置应用程序。
答案 2 :(得分:0)
请按照以下步骤操作:
1. Go to facebook developer
2. Select your app
3. Click "Status & Review" tab. Switch it to "YES" option