如何在Ballerina客户端凭证授权类型中添加额外认证参数?
问题:Ballerina中为OAuth2 Client Credentials模式添加audience参数
需要发送的OAuth2 token请求如下:
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"}'
当前使用的Ballerina客户端代码:
http:Client securedEP = check new ("http://postman-echo.com", { auth: { tokenUrl: "xxx/oauth/token", clientId: "xxx", clientSecret: "xxx", scopes: ["read", "submit"] } });
服务返回错误:
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"}'
解决方案
在Ballerina的http:Client配置的auth对象中,直接添加audience字段即可,该字段对应OAuth2 Client Credentials模式请求中需要的audience参数。修改后的代码如下:
http:Client securedEP = check new ("http://postman-echo.com", { auth: { tokenUrl: "xxx/oauth/token", clientId: "xxx", clientSecret: "xxx", scopes: ["read", "submit"], audience: "your-audience-value" // 替换为实际的audience值 } });
当使用Client Credentials授权类型时,Ballerina的HTTP客户端会自动构造包含audience参数的token请求,匹配你提供的curl请求格式,从而解决服务返回的403错误。
内容的提问来源于stack exchange,提问作者Rukshan Perera
相关产品推荐
相关产品推荐

