Search code examples
authenticationballerinaballerina-http

Adding extra auth parameter to client credentials grant type in Ballerina?


I need to send a request like the one below.

curl --request POST --url https://dev-jlsubxnitkpok2tw.au.auth0.com/oauth/token 
--header 'content-type: application/json' \
--data '{"client_id":"","client_secret":"","audience":"","grant_type":"client_credentials"}'

I am using Ballerina like below

http:Client securedEP = check new ("http://postman-echo.com", {
            auth: {
                tokenUrl: "xxx/oauth/token",
                clientId: "xxx",
                clientSecret: "xxx",
                scopes: ["read", "submit"]
            }
        }

I get an error like the one below from the service.

cause: Failed to get a success response from the endpoint. Response code: '403'. 
Response body: '{"error":"access_denied","error_description":"No audience 
parameter was provided, and no default audience has been configured"}'

How can I achieve this in Ballerina?


Solution

  • You can use the optionalParams parameter in ClientCredentialsGrantConfig to specify that.

    http:Client securedEP = check new ("http://postman-echo.com", {
            auth: {
                tokenUrl: "xxx/oauth/token",
                clientId: "xxx",
                clientSecret: "xxx",
                scopes: ["read", "submit"],
                optionalParams: {
                    "audience": "aud"
                }
            }
        });
    

    Note that the auth record field here is coming from CommonClientConfiguration type.