我正在构建一个应用程序,该应用程序将支持FaceBook
。问题是登录。如果原始FaceBook
应用程序未安装在电话上,登录正在通过自定义对话框,一切正常,但是,如果安装了官方FaceBook
应用程序,则登录将通过自定义对话框进行,并且自动重定向到原始FaceBook
应用程序,然后什么也没发生。我已经在不同的手机上测试过,并且总是是同一问题。我的代码是
public class Login extends Activity {
public static final String mAPP_ID = getString(R.string.APP_ID);
public Facebook mFacebook = new Facebook(mAPP_ID);
String cus_id, cus_mob, cus_name, cus_points, success,fb_id;
SessionManager session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.LoginButton)).setOnClickListener(loginButtonListener);
SessionStore.restore(mFacebook, this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
private OnClickListener loginButtonListener = new OnClickListener() {
public void onClick(View v) {
if (!mFacebook.isSessionValid()) {
Toast.makeText(Login.this, "Authorizing", Toast.LENGTH_SHORT).show();
mFacebook.authorize(Login.this, new String[] { "" },
new LoginDialogListener());
} else {
Toast.makeText(Login.this, "Has valid session",Toast.LENGTH_SHORT).show();
try {
JSONObject json = Util.parseJson(mFacebook.request("me"));
String facebookID = json.getString("id");
String firstName = json.getString("first_name");
String lastName = json.getString("last_name");
Toast.makeText(
Login.this,
"You already have a valid session, " + firstName
+ " " + lastName
+ ". No need to re-authorize.",
Toast.LENGTH_SHORT).show();
Intent in = new Intent(getApplicationContext(),
Userpage1.class);
startActivity(in);
} catch (Exception error) {
Toast.makeText(Login.this, error.toString(),
Toast.LENGTH_SHORT).show();
} catch (FacebookError error) {
Toast.makeText(Login.this, error.toString(),
Toast.LENGTH_SHORT).show();
}
}
}
};
public final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
try {
// The user has logged in, so now you can query and use their
// Facebook info
JSONObject json = Util.parseJson(mFacebook.request("me"));
String facebookID = json.getString("id");
String firstName = json.getString("first_name");
String lastName = json.getString("last_name");
Toast.makeText(
Login.this,
"Thank you for Logging In, " + firstName + " "
+ lastName + "!", Toast.LENGTH_SHORT).show();
SessionStore.save(mFacebook, Login.this);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fb_id", facebookID));
String response = null;
try {
response = LoginHttpClient
.executeHttpPost(
"http://10.0.2.2/Upshot_Loyalty_Program/android_api/get_fb_id.php",
postParameters);
JSONObject json1 = new JSONObject(response);
JSONArray jArray = json1.getJSONArray("customer");
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
success = json_data.getString("success");
cus_id = json_data.getString("cus_id");
cus_name = json_data.getString("cus_name");
cus_points = json_data.getString("cus_points");
// User_List.add(json_data.getString("cus_id"));
}
} catch (Exception e) {
}
if (success.equals("1")) {
session = new SessionManager(getApplicationContext());
session.createLoginSessionRemMe(cus_id, cus_name,
cus_points);
Intent i = new Intent(getApplicationContext(),
Userpage1.class);
startActivity(i);
} else {
Intent i = new Intent(getApplicationContext(),
Mobileno.class);
i.putExtra("fb_id", facebookID);
startActivity(i);
}
} catch (Exception error) {
Toast.makeText(Login.this, error.toString(), Toast.LENGTH_SHORT)
.show();
} catch (FacebookError error) {
Toast.makeText(Login.this, error.toString(), Toast.LENGTH_SHORT)
.show();
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText(Login.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText(Login.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText(Login.this,
"Something went wrong. Please try again.",
Toast.LENGTH_LONG).show();
}
}
}
我有相同的问题,并以以下方式解决了问题。由于Facebook Android SDK文档:您将创建的每个Android应用都将被签名,并且您将需要在每个应用程序的键上注册Facebook,以作为真实性的安全检查。要绕过此应用程序的检查,您必须将键哈希添加到Facebook开发人员资料中。
要在本地计算机上生成密钥哈希,请运行Java的键盘实用程序(应该在您的控制台的路径上),以针对Android密钥库(开发过程中的调试密钥库,生产发布)。对于Windows运行的调试密钥库:
keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%.androiddebug.keystore | openssl sha1 -binary | openssl base64
然后转到您在Facebook上的开发人员设置,从左侧的导航中选择您的应用程序,然后将密钥哈希添加到您的个人资料中。您现在可以成功运行您的应用程序。
有关更多详细说明,请关注:Facebook Android SDK并开始从段落中阅读"简单地说,您将创建的每个Android应用都将被签署,您将需要注册每个应用程序的密钥..."