Hristo Vrigazov
Hristo Vrigazov

Reputation: 1407

How to enable Facebook authentication for Codename One on Android

I have been struggling with this for a few hours. Here's the steps I take:

  1. Create a new bare bones Codename One app with main package com.recipes.auth and main class FacebookAuth

  2. Create facebook app for android, setting package name to com.recipes.auth and setting the main class name to FacebookAuthSub

  3. Then, generate hash and paste it. The hash is created using:

    keytool -exportcert -alias (your_keystore_alias) -keystore (path_to_your_keystore) | openssl sha1 -binary | openssl base64

  4. Add build hint in Codename One facebook.appId

  5. Write the following code in the main form:

    String clientId = "xxxxx";
    String redirectURI = "{server}/auth/facebook";
    String clientSecret = "xxx";
    Login fb = FacebookConnect.getInstance();
    fb.setClientId(clientId);
    fb.setRedirectURI(redirectURI);
    fb.setClientSecret(clientSecret);
    //Sets a LoginCallback listener
    fb.setCallback(new LoginCallback() {
        @Override
        public void loginSuccessful() {
            ConnectionRequest connectionRequest = new ConnectionRequest();
            connectionRequest.setPost(false);
            connectionRequest.setUrl(
                    "{server}/auth/facebook/callback?access_token="
            + fb.getAccessToken().getToken());
            connectionRequest.addResponseListener(event -> {
                Log.p(new String(connectionRequest.getResponseData()));
            });
            NetworkManager.getInstance().addToQueue(connectionRequest);
        }
    
        @Override
        public void loginFailed(String errorMessage) {
            Dialog.show("No!", "it did not work!", "sad", null);
        }
    });
    
    Form hi = new Form("Hi World");
    Button loginWithFacebook = new Button("Fb Auth");
    loginWithFacebook.addActionListener(event -> {
        //trigger the login if not already logged in
        if(!fb.isUserLoggedIn()){
            fb.doLogin();
        }else{
            //get the token and now you can query the facebook API
            String token = fb.getAccessToken().getToken();
        }
    });
    hi.add(loginWithFacebook);
    hi.show();
    

    where {server} is my server's uri. The auth path of my server is {server}/auth/facebook, and the callback URL is {server}/auth/facebook/callback and the success redirect is {server}/api/Users/me.

  6. Add web client to the Facebook app, to enable the simulator

  7. On facebook app page in products, add product, and then add Facebook Login with my server entered in Valid OAuth redirect URIs.

  8. Now when I try from the device, I get an error about invalid hash.

    In one of these answers, it is suggested that this could be because

Android Key hash will change if you build apk from another device(PC)

Which in fact is correct since I am using Codename One cloud builds. I followed the following guides: Facebook Login - Codename One, Social Chat Part 3 - Codename One and could not find an answer. But how to setup everything related to Facebook authentication with my own server? I am looking for a simple, step by step approach that actually works.

Upvotes: 1

Views: 372

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

Don't believe all the stuff you read on stackoverflow...

Different computers will produce the same SHA1 otherwise encryption and digital signing wouldn't work!

Just to be sure I tried this with my Mac and Linux machines and got the exact same result. Facebook is flaky though with the SHA1 and it's possible that the JVM differences (JCA installs etc.) cause something. We sometimes connect the device to get the actual SHA1 values from the error logs but once we get the correct values they never change despite building with multiple servers.

Upvotes: 0

Related Questions