DEV Community

4

Identity Server 4 - Invalid HTTP request for token endpoint

Nesse post eu mostro a solução encontrada para um problema, que não está descrito na documentação do Identity Server 4 - Token Endpoint.

Problema:

Ao seguir a documentação e tentar obter um token válido no endpoint /connect/token recebo o seguinte retorno:

warn: IdentityServer4.Endpoints.TokenEndpoint[0]
      Invalid HTTP request for token endpoint
Enter fullscreen mode Exit fullscreen mode

Para testar a obtenção de um token válido, utilizei o Postman, com uma request do tipo POST conforme a seguir:
postman failed request to identity server 4 connect token

Veja que o retorno foi um erro:

{
    "error": "invalid_request"
}
Enter fullscreen mode Exit fullscreen mode

No servidor, esse é o client que foi inicializado no Identity Server 4:

// resource owner password grant client
clientList.Add(

new Client {
  ClientId = "ro.client",
  AllowedGrantTypes = GrantTypes.ResourceOwnerPasswordAndClientCredentials,

  ClientSecrets = {
    new Secret("t%n6CFC$h4XckGRna%=c2xrECt9M8v)NbF>)}x*9sF6ka3prLj.ozo3HELTGYPm9".Sha256())
  },
  AllowedScopes = {
    "openid",
    "apiErp.access",
    "apiControlPanel.access"
  },
  AllowOfflineAccess = true
}

);
Enter fullscreen mode Exit fullscreen mode

O estranho é que antes de atualizar o Identity Server 4 para a versão mais recente, estava funcionando perfeitamente essa request.

Solução:

Troquei o content-type da request de: form-data para: x-www-form-urlencoded.

Feito isso o resultado:

{
    "access_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjA4QkEyN0M0MUU4MTJBQUZGMjcyN0ExMEM5RTVCRERFIiwidHlwIjoiYXQrand0In0.eyJuYmYiOjE1OTY0NzY5MzYsImV4cCI6MTU5NjQ4MDUzNiwiaXNzIjoiaHR0cHM6Ly9sb2NhbGhvc3Q6NTAwMCIsImF1ZCI6ImFwaUVycCIsImNsaWVudF9pZCI6InJvLmNsaWVudCIsInN1YiI6IkFwcFVzZXJzL2dhYnJpZWxyYi5uZXRAZ21haWwuY29tIiwiYXV0aF90aW1lIjoxNTk2NDc2OTM2LCJpZHAiOiJsb2NhbCIsImp0aSI6IjZGNzRBNUYxRURBQzM3NDAzQTMwMTA0NUZDMzgyRjcwIiwiaWF0IjoxNTk2NDc2OTM2LCJzY29wZSI6WyJhcGlFcnAuYWNjZXNzIl0sImFtciI6WyJwd2QiXX0.dms9yFDZk7mTxovGsVxtHVokJBOieAbygovMdlLsoOqY_MsZ-kLxs-OHovrel_sHuBnaZhZG4FnwhV429-otQ-7jDCWGYKMIzscoLjx3DgQKQGFTXZJ-StU3qqYJYsYyGrtaUfQE7D3QXjBL6WCfdeuf2or8b1HABXIR4-GRhvhSvQwZpiApdTi30iKpbDHB_aLJyqmOo_IjiO1A78tdgq60ydbyyPsPrqfwh5yghDs8ZlE77JBKOzyWxqDRhFey_0wBVx37rbnV4UMyJ_rnVyM7Vn4tm5_0Un34uzCoS_jyehxv28bEOH_K1BR60rEmFArexKNolTC9OGCPjDTslQ",
    "expires_in": 3600,
    "token_type": "Bearer",
    "scope": "apiErp.access"
}
Enter fullscreen mode Exit fullscreen mode

Foi então que resolvi checar a documentação de referência do OpenId, e encontrei na especificação o seguinte modelo de request:

POST /token HTTP/1.1
  Host: server.example.com
👉Content-Type: application/x-www-form-urlencoded 
  Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW

  grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA
    &redirect_uri=https%3A%2F%2Fclient.example.org%2Fcb
Enter fullscreen mode Exit fullscreen mode

Vou tentar editar a documentação oficial do Identity Server 4, incluindo essa informação sobre o content type.

Espero que tenha te ajudado.
Valeu.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (2)

Collapse
 
caiocesar profile image
Caio Cesar • Edited

No meu caso estava configurando .AddInMemoryApiResources() tive que troca para .AddInMemoryApiScopes().

Collapse
 
brunobrant profile image
Bruno Brant

Muuuuito obrigado! Nenhum lugar achei documentado que a API do IdentityServer era via x-form-urlencoded

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more