<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=windows-1252">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hello everybody,<br>
    <br>
    I'm struggling to implement a keycloak login from my Android app.
    What I want to do is to simulate what a browser does (request a
    resource, be redirected to keycloak login page and get an access
    token later on). From what I've seen from the OAuth2 protocol, I
    need to firstly get the authorization code and later on request the
    access token with that code. I wouldn't like my Android app to be
    registered as a client itself, so I've registered my web service as
    public.<br>
    <br>
    Here I start the intent to launch the Android browser pointing to
    the keycloak authorization address (/openid-connect/auth):<br>
    <br>
    Intent i = new Intent(Intent.ACTION_VIEW,
    Uri.parse(<a class="moz-txt-link-rfc2396E" href="http://192.168.0.230:8080/auth/realms/master/protocol/">"http://192.168.0.230:8080/auth/realms/master/protocol/"</a> +<br>
       
"openid-connect/auth?response_type=code&amp;client_id=web_service&amp;redirect_uri=android://app"));<br>
    startActivity(i);<br>
    <br>
    When I enter the credentials successfully, I'm given a code in the
    redirection url, coming back to my app:<br>
    <br>
    Uri data = getIntent().getData();<br>
    if (data != null) {<br>
        String accessCode= data.getQueryParameter("code");<br>
        System.out.println("Authorization code:" + accessCode);<br>
    }<br>
    <br>
    The code looks like
bC8OM0TwfbhYouM34M3RY2aZRYTywXZVAvKFABgvVRc.693363de-d6b5-4f88-b545-c6603023ee4c<br>
    <br>
    Then I would like to get an access token with that code! I use a
    Rest Template for that, making a POST request to the
    /openid-connect/token endpoint:<br>
    <br>
    ResponseEntity rssResponse = template.exchange(<br>
            <a class="moz-txt-link-rfc2396E" href="http://192.168.0.230:8080/auth/realms/master/protocol/">"http://192.168.0.230:8080/auth/realms/master/protocol/"</a> +<br>
                   
"openid-connect/token?grant_type=authoritation_code&amp;client_id=web_service&amp;code="+accessCode,<br>
            HttpMethod.POST,<br>
            null,<br>
            Object.class);<br>
    <br>
    However, I'm given a 401 unauthorized exception. What am I doing
    wrong? Thanks in advanced ;-)<br>
    <br>
    <div class="moz-signature">-- <br>
      <meta http-equiv="content-type" content="text/html;
        charset=windows-1252">
      <div class="moz-signature">
        <table style="cellspadding: 0; width: 600; align: left;
          border-collapse: collapse;">
          <tbody>
            <tr>
              <td style="border-bottom-width: 1px; border-bottom-style:
                solid; border-bottom-color: #989898;"> <span
                  style="font-weight:bold">Aritz Maeztu Otaņo</span><br>
                <span style="font-size: 12px;">Departamento Desarrollo
                  de Software</span> </td>
              <td style="border-bottom-width: 1px; border-bottom-style:
                solid; border-bottom-color: #989898; padding-left:
                20px;"> <a target="_blank"
                  href="https://www.linkedin.com/profile/preview?vpa=pub&amp;locale=es_ES">
                  <img src="cid:part1.06000508.08070803@tesicnor.com"
                    border="0">
                  <!--<img src="linkdin.gif" border="0" />--> </a> </td>
            </tr>
            <tr>
              <td> <a target="_blank" href="http://www.tesicnor.com"> <img
                    shrinktofit="true"
                    src="cid:part3.04060608.00070409@tesicnor.com"
                    border="0" width="143">
                  <!--<img shrinktofit="true" src="logo.png" width="143" border="0" />-->
                </a> </td>
              <td style="font-size: 12px;">
                <p style="padding-left: 20px;"> <span>Pol. Ind.
                    Mocholi.</span> <span>C/Rio Elorz, Nave 13E </span><span
                    style="font-weight:bold">31110 Noain (Navarra)</span><br>
                  <span>Telf.: 948 21 40 40</span> <br>
                  <span>Fax.: 948 21 40 41</span> <br>
                </p>
              </td>
            </tr>
            <tr>
              <td colspan="2"> <span style="color: #009900;font-size:
                  12px;">Antes de imprimir este e-mail piense bien si es
                  necesario hacerlo: El medioambiente es cosa de todos.</span>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </body>
</html>