if(session.isOpen()), facebook login on android always returning false

前端 未结 4 1610
粉色の甜心
粉色の甜心 2020-12-15 08:25

I\'m trying to implement a simple activity in my android application where a user is asked to login via facebook so that the user\'s \'likes\' are retrieved. So obviously fi

相关标签:
4条回答
  • 2020-12-15 08:47
    private Session.StatusCallback statusCallback = new SessionStatusCallback();
    
    private class SessionStatusCallback implements Session.StatusCallback {
        @Override
        public void call(final Session session, SessionState state,
                Exception exception) {
    
            try {
                new_session = session;
                if (state.equals(SessionState.OPENING)) {
    
                }
    
                if (state.equals(SessionState.OPENED)) {
    
    
                }
    
                if (state.equals(SessionState.CLOSED_LOGIN_FAILED)) {
                    try {
                        PackageInfo info = context.getPackageManager()
                                .getPackageInfo("your package name",
                                        PackageManager.GET_SIGNATURES);
                        for (Signature signature : info.signatures) {
                            MessageDigest md = MessageDigest.getInstance("SHA");
                            md.update(signature.toByteArray());
                            Log.d("KeyHash:", Base64.encodeToString(
                                    md.digest(), Base64.DEFAULT));
                        }
                        Session.openActiveSession((Activity) context, true,
                                statusCallback);
                    } catch (NameNotFoundException e) {
    
                    } catch (NoSuchAlgorithmException e) {
    
                    }
                }
    
                if (state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
    
    
    
                }
    
        }
        catch(Exception e){}
      }
    }
    
    //if session is closed
    Session.openActiveSession((Activity) context, true,
                        statusCallback);
    
    0 讨论(0)
  • 2020-12-15 08:55

    I had the similar problem it was the hashkey was wrong in facebook, by following the below code you can get the hash key that has been sent to facebook. Just copy this hashkey and replace it. It will start working.

        try {
            PackageInfo info = getPackageManager().getPackageInfo(
                    "your.root.package", 
                    PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                }
        } catch (NameNotFoundException e) {
    
        } catch (NoSuchAlgorithmException e) {
    
        }
    
    0 讨论(0)
  • 2020-12-15 08:58

    You have to add the key hash on Facebook developer it works for me

    0 讨论(0)
  • 2020-12-15 09:07

    You're missing the onActivityResult override (the last bit of the code snippet in the Getting Started Guide).

    The onActivityResult is how information gets from the FB app (or the webview) back into your app, and must be overridden to call back into the session.

    0 讨论(0)
提交回复
热议问题