<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Many thanks to all of you for the help. I'm so close to achieve it,
    so I need some last tip (and think you can do even about not to have
    mobile knowledge). That's the steps I've followed to authenticate a
    user in a public client in the Android app:<br>
    <br>
    1- Launch a browser app pointing to keycloak's authorization site
    for the client:<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/"><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></a>
    +<br>
       
"openid-connect/auth?response_type=code&amp;client_id=web_service&amp;redirect_uri=android://app"));<br>
    startActivity(i);<br>
    <br>
    2- Retrieve the authorization code when coming back to my app and
    ask for an access token:<br>
    <br>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
                RestTemplate template = new RestTemplate();<br>
                template.getMessageConverters().add(new
    FormHttpMessageConverter());<br>
                template.getMessageConverters().add(new
    MappingJackson2HttpMessageConverter());<br>
                MultiValueMap&lt;String, String&gt; form = new
    LinkedMultiValueMap&lt;&gt;();<br>
                form.add("grant_type", "authorization_code");<br>
                form.add("client_id", "edge");<br>
                form.add("code", accessCode);<br>
                form.add("redirect_uri", "tcheck://app");<br>
                ResponseEntity rssResponse = template.postForEntity(<br>
                       
<a class="moz-txt-link-rfc2396E" href="http://192.168.0.230:8080/auth/realms/master/protocol/openid-connect/token">"http://192.168.0.230:8080/auth/realms/master/protocol/openid-connect/token"</a>,<br>
                        form,<br>
                        AccessToken.class);<br>
    <br>
    I'm passing the parameters in the request body as
    x-www-form-urlencoded and it works. I do get an access token, with
    this format:<br>
    <br>
    {<br>
        "access_token" :
"eyJhbGciOiJSUzI1NiJ9.eyJqdGkiOiI5OTEzYmRjOS1jZmI0LTRlZjAtYTcxYy0yYWUwYmQ3MTkwZDkiLCJle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zZXNzaW9uX3N0YXRlIjoiN2RkNWFkN2ItZDBhYi00ZmJhLWI5Y2ItZjM2NjFhOTk0ZTc5IiwibmFtZSI6IiIsInByZWZlcnJlZF91c2VybmFtZSI6ImFkbWluIn0.yOs1HGLQyV33ihDIzL4CiKlKj58zlZzNpJizOlWXg59DkdnL1W5RIT4-Jw5VToy267gWv1o0XIwI2oCVHjbaXKgWZzt7NlVdGnNyGL19VQUPlISlMyyoOhaBGufC4JycQ6BrQh0fnMYUVQOvGE6HGnVwUbrLHiVL579AVhUSmVZ052fzN4VySpm03L7eQBt6BTKMo_7fmL39WvdwY2gEhoi6rz2P8cXp8vbidwqb4nNF7C1wfM7GYgbO-1yaMq_c4JiOoga9YswD68XvKpjjwVZs2WvHpvwZrQjfiqa6EtxkTeRYncMW-RutB8P09wJ3WRaBooDreVBMFB2Tw6nWnQ",<br>
        "not-before-policy" : 1452694301,<br>
        "session-state" : "7dd5ad7b-d0ab-4fba-b9cb-f3661a994e79"<br>
    }<br>
    <br>
    I now finally want to access some resource. As docs state, the only
    thing I want to do is to pass that access token in the Authorization
    header, starting with the Bearer keyword:<br>
    <br>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
                HttpHeaders headers = new HttpHeaders();<br>
                headers.set("Authorization", "Bearer " +
    token.getToken());<br>
                HttpEntity&lt;String&gt; entity = new
    HttpEntity&lt;&gt;("parameters", headers);<br>
                ResponseEntity rssResponse = template.exchange(<br>
                       
    <a class="moz-txt-link-rfc2396E" href="http://192.168.0.230:8765/organization/organizations">"http://192.168.0.230:8765/organization/organizations"</a>,<br>
                        HttpMethod.GET,<br>
                        entity,<br>
                        OrganizationExchangeSet.class);<br>
    <br>
    But I get 401 Unauthorized from keycloak. If I do the GET request
    using Postman, I get the Unauthorized code too:<br>
    <br>
    Request:<br>
    <br>
    <i>Url:</i><br>
    <br>
    <a class="moz-txt-link-freetext" href="http://192.168.0.230:8765/organization/organizations">http://192.168.0.230:8765/organization/organizations</a><br>
    <br>
    <i>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mFnZS1hY2NvdW50Iiwidmlldy1wcm9maWxlIl19fSwibmFtZSI6IiIsInByZWZlcnJlZF91c2VybmFtZSI6ImFkbWluIn0.GMoAPe9aUQBRign5J0TvOt4tg1SWwyfJkvJjuWDZ_Ayj3GBnFjhgbjb5qLreKsm87NHymPcpvCv7uHkKJRsx44TjC0514O0oBSiVIiKfcJdbE-y7nPplzYAJF6I2JlsQkw9Na67vNSvhsBNg6AfBop4xpAF9HtTU7Ca7gFwOS01bgDRO09WlJYivzOd5t-vQGNwRVlTqaCstIMiBLaUfdkc82DNQwnoP5VO9R7xZn-7O5BE288_CX0C2V96_vooIoTbB3Qoa-gV6f3s6ZSyJnRGBgoe_2QY3mjCBarFQ_mKH_sbF2qMpm-a5igoNoD_3Xlc7iluP206ZJdQn4NZdQg<br>
    <br>
    <i>Response:</i><br>
    <br>
    {<br>
      "timestamp": 1452784544622,<br>
      "status": 401,<br>
      "error": "Unauthorized",<br>
      "message": "Unable to authenticate bearer token",<br>
      "path": "/organization/organizations"<br>
    }<br>
    <br>
    How to solve this?<br>
    <br>
    <br>
    <div class="moz-signature">-- <br>
      <meta http-equiv="content-type" content="text/html; charset=utf-8">
      <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:part2.04040402.00070607@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:part4.08060005.08000406@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>