[英]Can't Log in with Facebook when I export my application
I built an App with the Facebook SDK, everything was working fine on Emulator, but when I exported my app to Run on my mobile device, I couldn't do the log in, its frustrating because this is my first App and I thought that everything was working without errors. 我使用Facebook SDK构建了一个应用程序,在Emulator上一切正常,但是当我将应用程序导出到在移动设备上运行时,我无法登录,令人沮丧的是,这是我的第一个应用程序,我认为一切正常,没有错误。
My Login in works as a registering button too. 我的登录名也可以作为注册按钮。 When someone without an account tries to login, it automatically creates an account using the data from Facebook. 当没有帐户的人尝试登录时,它会使用来自Facebook的数据自动创建一个帐户。
I'm using PHP to insert data into my Database, I'm calling this function when someone tries to Log in: 我使用PHP将数据插入数据库中,有人尝试登录时调用此函数:
case "loginfb":
facebook_func($_POST['email'],$_POST['name'],$_POST['gender'],$_POST['image']);
My function calls a Query to check if the sent E-mail already exists to do the normal login, else, it creates a new account. 我的函数调用查询来检查发送的电子邮件是否已经存在以进行常规登录,否则,它将创建一个新帐户。
function facebook_func($email,$name,$gender,$image){
$result = db_queries("SELECT * FROM users WHERE email='$email'");
$row = mysqli_fetch_array($result);
if($row[16] == 1){ //row 16 check if the account uses social network login
if($row[0]){ //Check if the id exists on database
echo $row[0];
online_func($row[3],$row[0]);
}else{
//Inserts new account into the Database
//echo id from the user
}
}
I'm echoing the Id from the user to save it in a Java session. 我正在回显用户的ID,以将其保存在Java会话中。
Session code: 会话代码:
public class NewSession extends Application {
Session session = new Session();
public String getUserId() {
return session.suserid;
}
}
My Main activity full code: 我的主要活动的完整代码:
public class MainActivity extends Activity {
private EditText email, password;
private TextView status;
private CallbackManager callbackManager;
private LoginButton loginButton;
@Override
protected void onActivityResult(int requestCode, int responseCode, Intent data) {
super.onActivityResult(requestCode, responseCode, data);
callbackManager.onActivityResult(requestCode, responseCode, data);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_main);
email = (EditText) findViewById(R.id.email);
password = (EditText) findViewById(R.id.password);
status = (TextView) findViewById(R.id.statusField);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
List < String > permissionNeeds = Arrays.asList("user_photos", "email");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager, new FacebookCallback < LoginResult > () {@Override
public void onSuccess(LoginResult loginResult) {
System.out.println("onSuccess");
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,
GraphResponse response) {
// TODO Auto-generated method stub
// Application code
Log.v("LoginActivity", response.toString());
//System.out.println("Check: " + response.toString());
try {
String id = object.getString("id");
String name = object.getString("name");
String image = "http://graph.facebook.com/" + id + "/picture?width=600&height=600";
String email = object.getString("email");
String gender = object.getString("gender");
String getgender = "";
if (getgender.equals("male")) {
getgender = "2";
} else {
getgender = "1";
}
facebookLogin(email, name, getgender, image);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
System.out.println("onCancel");
}
@Override
public void onError(FacebookException exception) {
System.out.println("onError");
Log.v("LoginActivity", exception.getCause().toString());
}
});
}
public void login(View view) {
String getemail = email.getText().toString();
String getpassword = password.getText().toString();
new SigninActivity(this, status).execute(getemail, getpassword);
}
public void facebookLogin(String email, String name, String gender, String image) {
new FBSigninActivity(this, status).execute(email, name, gender, image);
Toast.makeText(getBaseContext(), "...",
Toast.LENGTH_LONG).show();
}
}
My facebook login class 我的Facebook登录课程
public class FBSigninActivity extends AsyncTask < String, Void, String > {
private TextView statusField;
private Context context;
private static final int LONG_DELAY = 3500; // 3.5 seconds
public FBSigninActivity(Context context, TextView statusField) {
this.context = context;
this.statusField = statusField;
}
@Override
protected String doInBackground(String...arg0) {
try {
String getemail = (String) arg0[0];
String getname = (String) arg0[1];
String getgender = (String) arg0[2];
String getimage = (String) arg0[3];
String link = "http://www.website.pt/Android/index.php?section=loginfb";
String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(getemail, "UTF-8");
data += "&" + URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(getname, "UTF-8");
data += "&" + URLEncoder.encode("gender", "UTF-8") + "=" + URLEncoder.encode(getgender, "UTF-8");
data += "&" + URLEncoder.encode("image", "UTF-8") + "=" + URLEncoder.encode(getimage, "UTF-8");
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("user", "pass".toCharArray());
}
});
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
sb.append(line);
break;
}
return sb.toString();
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
if (result == "") {
Toast.makeText(context, "shows error",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "welcome message",
Toast.LENGTH_LONG).show();
String newuser = result.substring(0, 1);
String newid = result.substring(1);
if (newuser.equals("N")) {
Toast.makeText(context, "...",
Toast.LENGTH_LONG).show();
Session.suserid = newid;
Intent in = new Intent(context,
editProfile.class);
in .putExtra("checku", "new");
context.startActivity( in );
}else{
context.startActivity(new Intent(context, ListUsersActivity.class));
Session.suserid = result;
}
}
}
}
Thanks. 谢谢。
When you create a release apk, it's signed with a different keystore, so the first key hash that you created and put into the Facebook app settings won't work for the release apk. 创建发行版APK时,它会使用其他密钥库进行签名,因此您创建并放入Facebook应用程序设置的第一个密钥哈希不适用于发行版APK。
You need to get another key hash, for the new apk, and also add that to your settings as well. 您需要为新的APK获取另一个密钥哈希,并将其也添加到您的设置中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.