How to do oAuth with Keycloak OpenId in Android Client
<p>I want to authenticate a user (using his username and password) in an Android App using aerogear with a server using Keycloak. I haven't been able to do it, help me please.</p>

<p>I currently can authenticate the user without aerogear, but I want to use this library since it can help me to refresh the token when is needed.
I authenticate the user making a POST call to the server like this (but from android):</p>

<pre><code> curl -X POST http://127.0.0.1:8080/auth/realms/example/protocol/openid-connect/token  
 -H "Content-Type: application/x-www-form-urlencoded" -d "username=auser" -d 'password=apassword' -d 'grant_type=password' 
 -d 'client_id=clientId' -d 'client_secret=secret'
</code></pre>

<p>So the information I have is: </p>

<ul>
<li>Authentication URL,  <code>ie http://127.0.0.1:8080/auth/realms/example/protocol/openid-connect/token</code></li>
<li>username, the username of the user</li>
<li>password, the password of the user</li>
<li>client_id, and client_secret of the Keycloak server</li>
</ul>

<p>What I have tried with Aerogear is this:</p>

<pre><code>private void authz() {
    try {

        AuthzModule authzModule = AuthorizationManager.config("KeyCloakAuthz", OAuth2AuthorizationConfiguration.class)
                .setBaseURL(new URL("http://127.0.0.1:8080/"))
                .setAuthzEndpoint("/realms/example/protocol/openid-connect/auth")
                .setAccessTokenEndpoint("/realms/example/protocol/openid-connect/token")
                .setAccountId("keycloak-token")
                .setClientId("clientId")
                .setClientSecret("secret")
                .setRedirectURL("http://oauth2callback")
                .setScopes(Arrays.asList("openid"))
                .addAdditionalAuthorizationParam((Pair.create("grant_type", "password")))
                .addAdditionalAuthorizationParam((Pair.create("username", "aUserName")))
                .addAdditionalAuthorizationParam((Pair.create("password", "aPassword")))
                .asModule();


        authzModule.requestAccess(this, new Callback&lt;String&gt;() {
            @Override
            public void onSuccess(String o) {
                Log.d("TOKEN ", o);
            }

            @Override
            public void onFailure(Exception e) {
                System.err.println("Error!!");
                Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
            }
        });

    } catch (Exception e) {

        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
</code></pre>

<p>However this doesn't do anything. What I don't understand is:</p>

<ol>
<li>How can I specify that I'm doing and OpenID Connect with Keycloak in Aerogear?</li>
<li>How and where can I send the username and password?</li>
<li>How can I specify the grant_type? (My HTTP POST to the server does not work if I don't include this, so it's important)</li>
</ol>

<p>Any help would be very much appreciated</p>