我正在使用 FB SDK3,我的 Android 应用需要获取访问令牌。为此,我创建了以下代码:
private static class FacebookConnector implements Session.StatusCallback {
private static FacebookConnector instance = null;
private Fragment mFragment;
private Session session;
public static FacebookConnector getInctance(Fragment fragment) {
if (instance == null) {
instance = new FacebookConnector();
}
instance.setFragment(fragment);
return instance;
}
private FacebookConnector() {
}
/**
* Set the fragment that make the fb authentication
*
* @param fragment
*/
public void setFragment(Fragment fragment) {
mFragment = fragment;
}
/**
* Connect to facebook
*/
public void logIn() {
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
session = new Session.Builder(mFragment.getActivity())
.setApplicationId("548056975227881")
.build();
session.addCallback(this);
Session.setActiveSession(session);
// Login
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(mFragment).setCallback(this));
} else {
Session.openActiveSession(mFragment.getActivity(), true, this);
}
}
/**
* @return the access token, null if not exist
*/
public String getToken() {
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(mFragment).setCallback(this));
}
String token = session.getAccessToken();
System.out.println("TOKEN: " + token);
return token;
}
/**
* Disconnect from facebook
*/
public void logOut() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
@Override
public void call(Session session, SessionState state, Exception exception) {
String token = session.getAccessToken();
Log.d(state.toString(), "Token: " + token);
if (exception != null) {
Log.e("FACEBOOK", exception.getMessage(), exception);
}
Log.d("", "-----------------------");
}
}
它工作得很好,直到我得到那个例外:
4-14 18:32:14.502: E/FACEBOOK(26812): Invalid application MY_APP_ID
04-14 18:32:14.502: E/FACEBOOK(26812): com.facebook.FacebookOperationCanceledException: Invalid application MY_APP_ID
04-14 18:32:14.502: E/FACEBOOK(26812): at com.facebook.Session.handleAuthorizationResult(Session.java:1043)
04-14 18:32:14.502: E/FACEBOOK(26812): at com.facebook.Session.onActivityResult(Session.java:543)
04-14 18:32:14.502: E/FACEBOOK(26812): at com.maccabi.counteat.ui.welcome.WelcomeFragment.onActivityResult(WelcomeFragment.java:121)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:161)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.app.Activity.dispatchActivityResult(Activity.java:3988)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.app.ActivityThread.deliverResults(ActivityThread.java:2689)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2735)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.app.ActivityThread.access$2000(ActivityThread.java:124)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.os.Looper.loop(Looper.java:130)
04-14 18:32:14.502: E/FACEBOOK(26812): at android.app.ActivityThread.main(ActivityThread.java:3844)
04-14 18:32:14.502: E/FACEBOOK(26812): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 18:32:14.502: E/FACEBOOK(26812): at java.lang.reflect.Method.invoke(Method.java:507)
04-14 18:32:14.502: E/FACEBOOK(26812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-14 18:32:14.502: E/FACEBOOK(26812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-14 18:32:14.502: E/FACEBOOK(26812): at dalvik.system.NativeStart.main(Native Method)
我无法理解会发生什么。我所做的唯一更改是在 FB 端应用程序中添加 Facebook 评论框并使用 WebView 从其他片段访问它。在我看到下一篇文章之后。我正在我的设备(不是模拟器)上调试我的应用程序。
如果是因为FB单点登录还是其他原因?当我在我的设备上运行我的应用程序时,我如何才能实现这种变化?