简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM