Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 first I'm testing a simple login via facebook. I'm using the exact code they have on 'Getting Started with the Facebook SDK for Android' - Step 6 (https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/) but for some reason it's not working. When I debugged the code I realised that the following condition:

if(session.isOpen()) 

is ALWAYS returning false even though I'd already be logged onto facebook. I've tried everything to fix it but nothing seems to be working.

Anybody had this issue or knows how to fix it?

EDIT: I'm trying this test on my phone directly not on an emulator. I don't know if that could be an issue.

Thanks in advance!

And I did try this solution session.isOpened() returns false even if successfully logged in to Facebook but using java 6 or 7 still gave the same hash key.

EDIT 2: I've also tried the same thing but with a slightly different approach. http://sonyarouje.com/2011/09/18/facebook-hash-key-for-android-apps/ I'm still having the same problem. The session.isOpen() method is always returning false.

EDIT 3: Here is the latest code I've tried. I don't think there are any errors in the logcat. Anyway I'll link it here just in case I'm missing something.

package com.example.danandroidapp;
import java.util.Arrays;

import com.facebook.FacebookException;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
import com.facebook.widget.LoginButton.OnErrorListener;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

    private String TAG = "MainActivity";
    private TextView lblEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lblEmail = (TextView) findViewById(R.id.lblEmail);

        LoginButton authButton = (LoginButton) findViewById(R.id.authButton);

        authButton.setOnErrorListener(new OnErrorListener() {
            @Override
            public void onError(FacebookException error) {
                Log.i(TAG, "Error " + error.getMessage());
            }
        });

        authButton.setReadPermissions(Arrays.asList("basic_info", "email"));
        authButton.setSessionStatusCallback(new Session.StatusCallback() {

        @Override
        public void call(Session session, SessionState state, Exception exception) {
            if(session.isOpened()) {
                Log.i(TAG, "Access Token " + session.getAccessToken());
                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {

                    @Override
                    public void onCompleted(GraphUser user, Response response) {
                        if(user != null) {
                            Log.i(TAG, "User ID " + user.getId());
                            Log.i(TAG, "Email " + user.asMap().get("email"));
                            lblEmail.setText(user.asMap().get("email").toString());
                        }
                    }
                });
            }
        }
    });
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

And my logcat output: https://www.dropbox.com/s/7qg9zbhlpikfovf/log.txt

like image 211
Daniel Grima Avatar asked Apr 23 '13 18:04

Daniel Grima


2 Answers

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) {

    }
like image 179
Ranjith Avatar answered Nov 08 '22 02:11

Ranjith


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.

like image 6
Ming Li Avatar answered Nov 08 '22 02:11

Ming Li