Search code examples
asp.net-coreoauth-2.0jwtidentityserver4access-token

Always getting "invalid_client" from Identity Server


Everything seems normal but it is not working, It returns "Invalid_Client" - (400 - Bad request). Both side so simple below;

Identity Server Code:

                new Client
                {
                    ClientId = "js",
                    ClientSecrets = {
                        new Secret("secret".Sha256())
                    },
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    RequireClientSecret = false,
                    AllowedScopes =
                    {
                        "api1"
                    }
                }

Javascript Client Code:

axios.post('http://localhost:5000/connect/token',request, {
        headers: {
             'client_id' : 'js',
             'client_secret' : 'secret',
             'grant_type': 'client_credentials',
             'scope' : 'api1'

             }});

Solution

  • Should not use normall fetch or axis.

    Because the service is Oauth service and should be use 'client-oauth2' library.

    Code Example:

         var ClientOAuth2 = require("client-oauth2");
         var authRequest = new ClientOAuth2({
         clientId: IDENTITY_CONFIG.client_id,
         clientSecret: IDENTITY_CONFIG.client_secret,
         accessTokenUri: IDENTITY_CONFIG.token_endpoint,
         scopes: [IDENTITY_CONFIG.grantType]
      });
    
      return authRequest.credentials.getToken();