我已经从github下载并设置了android简单的facebook sdk。我做了一个简单的页面来测试发布提要,我可以添加一条消息并点击"分享"。然而,一旦我点击分享,就会出现一个进度对话框,上面写着"发送……",然后它就消失了,但什么也没发送。我打印了错误信息,上面写着"被用户取消"。
这是我的代码,它只是一个按钮,登录,并在成功尝试发布一个提要。
Button postStuff;
private UiLifecycleHelper uiHelper;
Permission[] permissions = new Permission[] {
Permission.USER_PHOTOS,
Permission.EMAIL,
Permission.PUBLISH_ACTION
};
SimpleFacebookConfiguration configuration = new SimpleFacebookConfiguration.Builder()
.setAppId("My-App-ID")
.setNamespace("my_namespace")
.setPermissions(permissions)
.build();
SimpleFacebook mSimpleFacebook;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fbtest);
SimpleFacebook.setConfiguration(configuration);
postStuff = (Button) findViewById(R.id.postStuff);
postStuff.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
OnLoginListener onLoginListener = new OnLoginListener() {
@Override
public void onLogin() {
// change the state of the button or do whatever you want
Log.i("fb login", "Logged in");
OnPublishListener onPublishListener = new OnPublishListener() {
@Override
public void onComplete(String postId) {
Log.i("fb post", "Published successfully. The new post id = " + postId);
}
@Override
public void onFail(String reason) {
Log.i("fb post","failed: " + reason);
}
/*
* You can override other methods here:
* onThinking(), onFail(String reason), onException(Throwable throwable)
*/
};
Feed feed = new Feed.Builder()
.setMessage("Check this out.")
.setName("Home Shop Guide App for Android")
.setCaption("Win prizes and deals daily")
.setDescription("User just won $500 from a daily scratchoff prize. You could be the next winner!")
.setPicture("https://homeshopguide.com/images/logo.png")
.setLink("https://homeshopguide.com/")
.build();
mSimpleFacebook.publish(feed, true, onPublishListener);
}
@Override
public void onNotAcceptingPermissions(Permission.Type type) {
// user didn't accept READ or WRITE permission
Log.w("fb login", String.format("You didn't accept %s permissions", type.name()));
}
@Override
public void onThinking() {
// TODO Auto-generated method stub
Log.i("fb login", "thinking");
}
@Override
public void onException(Throwable throwable) {
// TODO Auto-generated method stub
Log.i("fb login", "exception" + throwable.getLocalizedMessage());
}
@Override
public void onFail(String reason) {
// TODO Auto-generated method stub
Log.i("fb login", "failed: " + reason);
}
};
mSimpleFacebook.login(onLoginListener);
}
});
}
@Override
public void onResume() {
super.onResume();
mSimpleFacebook = SimpleFacebook.getInstance(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mSimpleFacebook.onActivityResult(this, requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
当我按下按钮时,错误日志中显示的是:
03-19 17:01:53.537: I/fb login(31226): Logged in
03-19 17:01:53.647: D/OpenGLRenderer(31226): Flushing caches (mode 1)
03-19 17:01:53.957: D/OpenGLRenderer(31226): Flushing caches (mode 0)
03-19 17:02:05.527: I/fb post(31226): failed: Canceled by user
编辑:第二天我又试了一次(在做任何修改之前),日志显示帖子成功了。然而,我正在使用的facebook帐户开发是一个企业帐户,我不知道饲料被张贴到哪里。
正如sromku建议的那样,我将应用程序置于实时模式,以正常的facebook帐户进行测试。我仍然收到"被用户取消"的错误。然而,当我检查我的墙时,帖子果然在那里。我现在的问题是,我做错了什么来接收这个错误,或者它只是一个错误,当它不应该出现。
此行为已在简单的facebook 2.2版本中修复。我一直在经历同样的问题,并在更新lib后得到修复。