Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session is always null in getLoginStatus #499

Closed
sonoman opened this issue May 31, 2014 · 17 comments
Closed

Session is always null in getLoginStatus #499

sonoman opened this issue May 31, 2014 · 17 comments

Comments

@sonoman
Copy link

sonoman commented May 31, 2014

After fighting with plugin installation, i managed to load the plugin. However, it seems is not completely working....i guess there is some code not being executed...

I have this code in my first controller in my Angular application

    if (window.cordova) {
        facebookConnectPlugin.getLoginStatus(
            function (status) {
                console.log("current status: " + JSON.stringify(status));
            },
            function (error) {
                console.log("Something went wrong: " + JSON.stringify(error));
            }
        );
    }

I'm debugging the application with Android Studio, and I can tell the plugin is executing the "initialize" method...and is creating a session (although disconnected).
But then it goes to the "execute" method with action = "getLoginStatus" (everything according to the code above) but session is null, and this line on the plugin:

callbackContext.success(Session.getActiveSession().getState().toString());

Of course, expodes with nullPointerException.

Any clue ?

Thanks.

@hiroferreira
Copy link

Same problem over here... Any solutions?

@sonoman
Copy link
Author

sonoman commented Jun 3, 2014

Ok, I fixed this. I just didn't close the issue because I think that it is still a bug (or there is something I don't understando on the sequence.

I went to ConnectPlugin.java, on facebook plugin, and change this piece of code on "initialize" method.

    Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
    if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
        Session.setActiveSession(session);
        // - Create the request
            Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());

by this one

    // Open a session if we have one cached
    Session session = new Session.Builder(cordova.getActivity()).setApplicationId(applicationId).build();
    Session.setActiveSession(session);
    if (session.getState() == SessionState.CREATED_TOKEN_LOADED) {
        // - Create the request
        Session.OpenRequest openRequest = new Session.OpenRequest(cordova.getActivity());

As you can notice, it's only one line that I moved up....still can't understand which one is the expected behavior of the developer if your first action after plugin initialization is "getLoginStatus"
Wel.....hope it helps

@hiroferreira
Copy link

Thank you!

@udfalkso
Copy link

udfalkso commented Jun 3, 2014

@sonoman Thanks! This fixed my issue as well.

@bgaillard
Copy link

👍 @sonoman Thanks it solved the issue in our app too !

@hiroferreira
Copy link

Now, for some weird reason, after calling the

facebookConnectPlugin.login(["basic_info", "email"],fb.loginSuccess,fb.loginFail);

I get no response. i Can get status but, when trying to login, it does not work. My app calls FB APP, which prompts the user to allow me or not to get basic_info and email, and after the user choose, android resumes my app but neither fail nor success js functions from facebookConnectPlugin.login are triggered... Did any of you had this issue? I've installed and reinstalled the pg.fb.plugin, master and develop branch, updated cordova, rm and add android platform, cleaned ant and android builds... Out of clues here... the only response i get when my app is resumed is

CordovaActivity: CB-3064: The errorUrl is null

And, as found in http://mail-archives.apache.org/mod_mbox/cordova-commits/201304.mbox/%3Cf8a2ab107b47474fbc2bd194b7d8bfeb@git.apache.org%3E , i can't get anymore information about this error. Other comment on errorUrl: http://mail-archives.apache.org/mod_mbox/cordova-issues/201304.mbox/%3CJIRA.12643218.1366292135593.203365.1366659915560@arcas%3E

Did any of you had this issue? Any clues on solving this?

@sonoman
Copy link
Author

sonoman commented Jun 4, 2014

Mmmm, can you put on your app some code to call loginStatus again, after executing logIn ?
which is the status message that you get ?

@godchuanz
Copy link

@sonoman may I ask which branch were you using? 0.5.1 develop?

@hiroferreira
Copy link

@sonoman before login the response to getLoginStatus is CREATED. Some seconds After login the response is CLOSED_LOGIN_FAILED.
I kept my eye on logcat and there is no answer from ConnectPlugin.java with token, user data or erros. I found out that it does registers callback functions through:

openRequest.setCallback(new Session.StatusCallback() {
                    @Override
                    public void call(Session session, SessionState state, Exception exception) {
                        onSessionStateChange(state, exception);
                    }
                });

And all further calls to:

private void onSessionStateChange(SessionState state, Exception exception) {
        final Session session = Session.getActiveSession();
        // Check if the session is open
        if (state.isOpened()) {
            if (loginContext != null) {
                // Get user info
                getUserInfo(session);
            } else if (graphContext != null) {
                // Make the graph call
                makeGraphCall();
            }
        }
}

results in closed state(state.isOpened() == false), what makes it never reach getUserInfo() nor makeGraphCall().
I also checkd the onActivityResult which calls the facebookSDK through the function

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        Log.d(TAG, "activity result in plugin");
                Session.getActiveSession().onActivityResult(cordova.getActivity(), requestCode, resultCode, intent);
}

And i found out that Session.getActiveSession().onActivityResult always returns true, but Session.getActiveSession().getAccessToken(), placed right after it, returns empty string... I have also tried to change only the ConnectPlugin.java file for some older ones, but they either didn't work or got the same error...
I am pretty lost...

@hiroferreira
Copy link

Just found my error!!! And i managed to do it thanks to your question about status after login @sonoman! Thank you again!
The problem is that my app was using a keyHash different from the one i had generated through terminal, as recommended by facebook at https://developers.facebook.com/docs/android/getting-started/
on section 4...
There is a code at http://stackoverflow.com/questions/15002701/sessionstate-is-closed-login-failed-in-fb-native-login that shows how to catch the right keyHash to be uploaded at your app's facebook dashboard!
To make it faster:

try {
    PackageInfo info = getPackageManager().getPackageInfo(
            "com.example.com.tvishi.fb", 
            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) {

}

Code above was written by Shahid Pasha (http://stackoverflow.com/users/1407064/shahid-pasha) and shows your apps Keyhashes

@sonoman
Copy link
Author

sonoman commented Jun 5, 2014

Cool :) I've asked you that because I had the same problem, and I solved
with the same code to get the actual hash used. Now that code goes always
on the default Android activity, just in case !
Well done :)
On Jun 5, 2014 1:39 AM, "Hiro Ferreira" notifications@github.com wrote:

Just found my error due to your question @sonoman
https://github.com/sonoman! Thank you again!
The problem is that my app was using a keyHash different from the one i
had generated through terminal, as recommended by facebook at
https://developers.facebook.com/docs/android/getting-started/
on section 4...
There is a code at
http://stackoverflow.com/questions/15002701/sessionstate-is-closed-login-failed-in-fb-native-login
that shows how to catch the right keyHash to be uploaded at you app's
facebook dashboard!
To make it faster:

try {
PackageInfo info = getPackageManager().getPackageInfo(
"com.example.com.tvishi.fb",
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) {
}

Code above was written by Shahid Pasha (
http://stackoverflow.com/users/1407064/shahid-pasha) and shows your apps
Keyhashes


Reply to this email directly or view it on GitHub
#499 (comment)
.

@aogilvie
Copy link
Collaborator

aogilvie commented Jun 8, 2014

#508 should fix the JSON error.

@aogilvie
Copy link
Collaborator

Main issue resolved.
If bad hash and no response from facebookConnectPlugin.login will be added to Troubleshooting Guide

@sebastianzillessen
Copy link
Contributor

Hi there!
I have the same issue! I can track the problem down to the same line:

    ....
        else if (action.equals("getLoginStatus")) {
        callbackContext.success(Session.getActiveSession().getState().toString());
        return true;
         }
   ....

My error message:

Uncaught exception from plugin
java.lang.NullPointerException
    at org.apache.cordova.facebook.ConnectPlugin.execute(ConnectPlugin.java:205)
    at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:65)
    at org.apache.cordova.PluginManager.execHelper(PluginManager.java:242)
    at org.apache.cordova.PluginManager.exec(PluginManager.java:227)
    at org.apache.cordova.ExposedJsApi.exec(ExposedJsApi.java:53)
    at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
    at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:24)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.os.HandlerThread.run(HandlerThread.java:61)

I checked my hashkey, they are all fine. Do you have any other ideas?

Edit
I'm working on version 0.5.1 which worked fine for another project. Then I copied the project and re-added the plugin and now I get this error.

@perilousleigh
Copy link

You need the code change proposed by sonoman at the top of this thread where he moves the line that sets the Active Session outside of an if block in the initialize() function. The change may be in the develop branch but if not you may have to make the change manually until it is (which is what I did to get things working).

@sebastianzillessen
Copy link
Contributor

Will there be a fix for that in version 0.6.0?

@aogilvie
Copy link
Collaborator

@sebastianzillessen it's on develop https://github.com/Wizcorp/phonegap-facebook-plugin/pull/584/files will push to master 0.7.0 shortly, just testing FB Events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants