我正在尝试按照Facebook Getting Started Guide.登录Facebook但是如果我的手机上也安装了Facebook应用程序,则session.isOpened()调用始终返回false。如果它没有安装在我的手机上,一切正常。我的代码如下:
public static void loginToFacebook(final Activity activity) {
Log.d("Hello", Global.debug + " Login to Facebook");
Session.openActiveSession(activity, true, new Session.StatusCallback() {
// callback when session changes state
@Override
public void call(Session session, SessionState state, Exception exception) {
Log.d("Session Changed", Global.debug);
if (session.isOpened()) {
Log.d("Session Is Opened", Global.debug);
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
Log.d("Request Completed", Global.debug);
if (user != null) {
Log.d("Hello ", Global.debug + " " + user.getName() + "!");
}
}
});
}
}
});
}
会话已更改打印到日志,但会话已打开,直到我删除Facebook应用程序,然后它打印与我的Facebook帐户关联的名称,就像我登录后应该这样。我环顾四周,发现其他具有相同问题的人使用了错误的Key Hash,并使用以下方法找到了正确的Key:
try {
PackageInfo info = getPackageManager().getPackageInfo("com.facebook.login", PackageManager.GET_SIGNATURES);
for (Signature signature : info.signatures) {
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(signature.toByteArray());
Log.d("KeyHash:", Global.debug + " " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
}
} catch (NameNotFoundException e) {
Log.d("Name Not Found", Global.debug);
} catch (NoSuchAlgorithmException e) {
Log.d("No Such Algorithm", Global.debug);
}
但那印刷了我已经使用过的Key Hash。有什么我做错了吗?非常感谢任何帮助,谢谢!