我正在尝试将 facebook 登录集成到我的应用程序中
我浏览了本教程:https ://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/
下载 facebook sdk 3.5
一步一步下载openssl,创建一个androidkeystore,生成hashkey,在facebook开发控制台中创建一个应用程序,给它我的包名,登录的活动,以及设备在日志控制台中打印给我的哈希码,如教程建议+我用openssl生成的hashkey,将app_id添加到字符串文件中,并将所需的权限活动和元数据添加到android清单文件中
现在我打开应用程序并单击“使用 facebook 登录按钮”它要求我对用户个人资料的许可,我单击确定
然后日志打印了这个异常:
10-16 19:51:20.718: W/Bundle(8444): Key com.facebook.platform.protocol.PROTOCOL_VERSION expected String but value was a java.lang.Integer. The default value <null> was returned.
10-16 19:51:20.718: W/Bundle(8444): Attempt to cast generated internal exception:
10-16 19:51:20.718: W/Bundle(8444): java.lang.ClassCastException: java.lang.Integer
10-16 19:51:20.718: W/Bundle(8444): at android.os.Bundle.getString(Bundle.java:1040)
10-16 19:51:20.718: W/Bundle(8444): at android.content.Intent.getStringExtra(Intent.java:3412)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.AuthorizationClient$KatanaLoginDialogAuthHandler.tryAuthorize(AuthorizationClient.java:829)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.AuthorizationClient.tryCurrentHandler(AuthorizationClient.java:278)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.AuthorizationClient.tryNextHandler(AuthorizationClient.java:244)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.AuthorizationClient$GetTokenAuthHandler.getTokenCompleted(AuthorizationClient.java:778)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.AuthorizationClient$GetTokenAuthHandler$1.completed(AuthorizationClient.java:737)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.internal.PlatformServiceClient.callback(PlatformServiceClient.java:144)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.internal.PlatformServiceClient.handleMessage(PlatformServiceClient.java:128)
10-16 19:51:20.718: W/Bundle(8444): at com.facebook.internal.PlatformServiceClient$1.handleMessage(PlatformServiceClient.java:54)
10-16 19:51:20.718: W/Bundle(8444): at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 19:51:20.718: W/Bundle(8444): at android.os.Looper.loop(Looper.java:130)
10-16 19:51:20.718: W/Bundle(8444): at android.app.ActivityThread.main(ActivityThread.java:3906)
10-16 19:51:20.718: W/Bundle(8444): at java.lang.reflect.Method.invokeNative(Native Method)
10-16 19:51:20.718: W/Bundle(8444): at java.lang.reflect.Method.invoke(Method.java:507)
10-16 19:51:20.718: W/Bundle(8444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:840)
10-16 19:51:20.718: W/Bundle(8444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:598)
10-16 19:51:20.718: W/Bundle(8444): at dalvik.system.NativeStart.main(Native Method)
这是一个警告,应用程序并没有因此而崩溃,但登录本身失败
这是我的登录流程代码:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
public class MainActivity extends FragmentActivity {
private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int FRAGMENT_COUNT = SELECTION + 1;
private Fragment[] fragments = new Fragment[FRAGMENT_COUNT];
private boolean isResumed = false;
private UiLifecycleHelper uiHelper;
private Session.StatusCallback callback =
new Session.StatusCallback() {
@Override
public void call(Session session,
SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
/**
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
transaction.hide(fragments[i]);
}
transaction.commit();
}
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
@Override
public void onResume() {
super.onResume();
uiHelper.onResume();
isResumed = true;
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
isResumed = false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
// Only make changes if the activity is visible
if (isResumed) {
FragmentManager manager = getSupportFragmentManager();
// Get the number of entries in the back stack
int backStackSize = manager.getBackStackEntryCount();
// Clear the back stack
for (int i = 0; i < backStackSize; i++) {
manager.popBackStack();
}
if (state.isOpened()) {
// If the session state is open:
// Show the authenticated fragment
showFragment(SELECTION, false);
} else if (state.isClosed()) {
// If the session state is closed:
// Show the login fragment
showFragment(SPLASH, false);
}
}
}
@Override
protected void onResumeFragments() {
super.onResumeFragments();
Session session = Session.getActiveSession();
if (session != null && session.isOpened()) {
// if the session is already open,
// try to show the selection fragment
showFragment(SELECTION, false);
} else {
// otherwise present the splash screen
// and ask the person to login.
showFragment(SPLASH, false);
}
}
}
我究竟做错了什么?为什么它不起作用?