title: How to enable Facebook authentication for Codename One on Android
body: <p>I have been struggling with this for a few hours. Here's the steps I take:</p>
  <ol>
  <li><p>Create a new bare bones Codename One app with main package <code>com.recipes.auth</code> and main class <code>FacebookAuth</code></p>
  </li>
  <li><p>Create facebook app for android, setting package name to <code>com.recipes.auth</code> and setting the main class name to <code>FacebookAuthSub</code></p>
  </li>
  <li><p>Then, generate hash and paste it. The hash is created using:</p>
  <p>keytool -exportcert -alias (your_keystore_alias) -keystore (path_to_your_keystore) | openssl sha1 -binary | openssl base64</p>
  </li>
  <li><p>Add build hint in Codename One facebook.appId</p>
  </li>
  <li><p>Write the following code in the main form:</p>
  <pre><code>String clientId = &quot;xxxxx&quot;;
  String redirectURI = &quot;{server}/auth/facebook&quot;;
  String clientSecret = &quot;xxx&quot;;
  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(
                  &quot;{server}/auth/facebook/callback?access_token=&quot;
          + fb.getAccessToken().getToken());
          connectionRequest.addResponseListener(event -&gt; {
              Log.p(new String(connectionRequest.getResponseData()));
          });
          NetworkManager.getInstance().addToQueue(connectionRequest);
      }
  
      @Override
      public void loginFailed(String errorMessage) {
          Dialog.show(&quot;No!&quot;, &quot;it did not work!&quot;, &quot;sad&quot;, null);
      }
  });
  
  Form hi = new Form(&quot;Hi World&quot;);
  Button loginWithFacebook = new Button(&quot;Fb Auth&quot;);
  loginWithFacebook.addActionListener(event -&gt; {
      //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();
  </code></pre>
  <p>where {server} is my server's uri. The auth path of my server is <code>{server}/auth/facebook</code>, and the callback URL is <code>{server}/auth/facebook/callback</code> and the success redirect is <code>{server}/api/Users/me</code>.</p>
  </li>
  <li><p>Add web client to the Facebook app, to enable the simulator</p>
  </li>
  <li><p>On facebook app page in products, add product, and then add Facebook Login with my server entered in Valid OAuth redirect URIs.</p>
  </li>
  <li><p>Now when I try from the device, I get an error about invalid hash.</p>
  <p>In one of <a href="https://stackoverflow.com/questions/35403055/facebook-error-invalid-key-hash-for-some-devices/35403132">these</a> answers, it is suggested that this could be because</p>
  </li>
  </ol>
  <blockquote>
  <p>Android Key hash will change if you build apk from another device(PC)</p>
  </blockquote>
  <p>Which in fact is correct since I am using Codename One cloud builds.  I followed the following guides:
  <a href="https://www.codenameone.com/facebook-login.html" rel="nofollow noreferrer">Facebook Login - Codename One</a>, <a href="https://www.codenameone.com/blog/building-a-chat-app-with-codename-one-part-3.html" rel="nofollow noreferrer">Social Chat Part 3 - Codename One</a>
  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.</p>