Facebook authentication works on an emulator, but not on a physical device

Hi to everyone who recently developed my first Android app that goes into facebook and tries to update status. Without a dialog using a graphical API. The code below is the authorization code (which was on the fbook dev website itself), and it worked fine for me all the time, until recently. Now my application is included in my emulator, but when I export the APK file and put it on my phone, it gives me an “authentication error”. Can someone explain this? It just doesn’t show me the login page anymore after I created a new keystore and hashkey and updated this hash on my facebook application developer page, as usual.

I think this is due to keyhash, etc., but I do not understand what is good enough to understand what happened. I do this, I click on export, the application, then create a new keystore (for the first time, otherwise I will use the existing keystore), then I find my hash key using "keytool exportcert", etc., as shown on fbook dev. Then I enter this hash into the application in my facebook account. But sometimes, even if the keystore password is correct, it says that the "keystore format" is different, although I used it before the same application! Then I need to create a new keystore and export again, and it all hurts! Should there be an easier way?

Can someone explain how hashkey works for Facebook apps?

Thanks in advance!

My authentication code:

public void login() { facebook.authorize(this,new String[] { "email", "read_stream", "publish_stream"}, new DialogListener(){ int fbcheck=0; @Override public void onComplete(Bundle values) { fbcheck=1; facebookauthcheck(fbcheck); } @Override public void onFacebookError(FacebookError error) { fbcheck=0; facebookauthcheck(fbcheck); } @Override public void onError(DialogError e) { fbcheck=0; facebookauthcheck(fbcheck); } @Override public void onCancel() { fbcheck=2; facebookauthcheck(fbcheck); } }); } public void facebookauthcheck(int fbcheck) { if (fbcheck == 0) { Toast.makeText(this, "Authentication Error", Toast.LENGTH_LONG).show(); } else if (fbcheck==1) { Toast.makeText(this, "Authenticated", Toast.LENGTH_LONG).show(); } else Toast.makeText(this, "Authentication Cancelled", Toast.LENGTH_LONG).show(); } 
+6
source share
2 answers

Yes, I had this problem,

It worked great on the emulator, worked fine on my mobile phone, but failed on the users test phone.

Do this with the Facebook app and SSO.

Read the following: http://sean.lyn.ch/2011/07/android-the-facebook-sdk-sso-and-you/

It has 3 solutions.

I could not get SSO to work, so I went for option two (exclude from SSO!) This is done by:

  facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, this); 

If you want to fix this and make SSO work:

Correctly create your hash key for the debug apk certificate. (described in detail in the link above).

Ref:

Relevant SO Question

+4
source

I use facebook login with fragments (replace "activity") with 'this':

  private void loginToFacebook() { mPrefs = activity.getPreferences(Context.MODE_PRIVATE); facebook = new Facebook(APP_ID); accessToken = mPrefs.getString("access_token", null); long expires = mPrefs.getLong("access_expires", 0); if (accessToken != null) { facebook.setAccessToken(accessToken); } if (expires != 0) { facebook.setAccessExpires(expires); } if (!facebook.isSessionValid()) { facebook.authorize(activity, new String[] {"publish_stream"}, Facebook.FORCE_DIALOG_AUTH, new DialogListener() { public void onFacebookError(FacebookError arg0) { // TODO Auto-generated method stub } public void onError(DialogError arg0) { // TODO Auto-generated method stub } public void onComplete(Bundle arg0) { SharedPreferences.Editor editor = mPrefs.edit(); editor.putString("access_token", facebook.getAccessToken()); editor.putLong("access_expires", facebook.getAccessExpires()); editor.commit(); uploadPhoto(facebook, bitmap, null); // calling another method to upload an image to FB after logging in.. } public void onCancel() { // TODO Auto-generated method stub } }); } } 
0
source

Source: https://habr.com/ru/post/886040/


All Articles