SessionEvents.AuthListener listener
应该听取登录成功。但它不听。每当我开始app时,它都会要求登录
下面是我的代码。
public class FacebookConnector {
private Facebook mFacebook;
private AuthListener mSessionListener;
private Context context;
private String[] permissions;
private Activity activity;
public FacebookConnector(String appId, Activity activity, Context context, String[] permissions) {
this.mFacebook = new Facebook(appId);
SessionStore.restore(mFacebook, context);
SessionEvents.addAuthListener(mSessionListener);
SessionEvents.addLogoutListener((LogoutListener) mSessionListener);
this.context = context;
this.permissions = permissions;
this.activity = activity;
}
public void postMessageOnWall(String msg) {
if (mFacebook.isSessionValid()) {
Bundle parameters = new Bundle();
parameters.putString("message", msg);
try {
String response = mFacebook.request("me/feed", parameters, "POST");
System.out.println(response);
} catch (IOException e) {
e.printStackTrace();
}
} else {
login();
}
}
public void login() {
if (!mFacebook.isSessionValid()) {
mFacebook.authorize(this.activity, this.permissions, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
}
public Facebook getFacebook() {
return mFacebook;
}
}
和我的活动课......
private void onFacebookBtnClicked() {
if (facebookConnector.getFacebook().isSessionValid()) {
postMessageInThread();
} else {
SessionEvents.AuthListener listener = new SessionEvents.AuthListener() {
@Override
public void onAuthSucceed() {
postMessageInThread();
}
@Override
public void onAuthFail(String error) {
}
};
SessionEvents.addAuthListener(listener);
facebookConnector.login();
}
}
private void postMessageInThread() {
Thread t = new Thread() {
public void run() {
try {
facebookConnector.postMessageOnWall(mQuote.getQuoteText() + "\n" + mQuote.getAuthorName());
mFacebookHandler.post(mUpdateFacebookNotification);
} catch (Exception ex) {
Log.e("Facebook", "Error sending msg", ex);
}
}
};
t.start();
}
final Runnable mUpdateFacebookNotification = new Runnable() {
public void run() {
Toast.makeText(getBaseContext(), "Facebook updated !", Toast.LENGTH_LONG).show();
}
};
在postMessageInThread
方法中没有执行,不知道为什么
答案 0 :(得分:1)
将此权限设置为公开
private String[] PERMISSIONS = new String[]
{ "publish_stream", "read_stream", "offline_access" };
这将检查会话是否存在,如果是,那么它将直接更新,如果没有,然后要求用户权限更新在墙上。
答案 1 :(得分:1)
嗨朋友们,我得到了解决方案,对于我的问题,所以现在发布答案。我只需要覆盖onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
我刚添加它并让我的应用程序正常工作。