【发布时间】:2020-04-23 16:09:22
【问题描述】:
我正在尝试调用使用 Oauth2 密码凭据的第 3 方服务来获取身份验证令牌。 Ballerina 正在返回以下消息。
2020-04-23 15:07:35,414 ERROR [ballerina/oauth2] - Received an invalid response with status-code: 406; and payload: {"fault":{"faultstring":"Raising fault. Fault name : RF.Raise-406-Exception","detail":{"errorcode":"steps.raisefault.RaiseFault"}}}
2020-04-23 15:07:35,418 ERROR [ballerina/oauth2] - Failed to generate OAuth2 token. : error {ballerina/oauth2}Error message=Received an invalid response with status-code: 406; and payload: {"fault":{"faultstring":"Raising fault. Fault name : RF.Raise-406-Exception","detail":{"errorcode":"steps.raisefault.RaiseFault"}}}
error {ballerina/http}AuthenticationFailed message=Failed to prepare request at bearer auth handler. cause=error {ballerina/auth}Error message=Failed to generate OAuth2 token. cause=error {ballerina/oauth2}Error message=Received an invalid response with status-code: 406; and payload: {"fault":{"faultstring":"Raising fault. Fault name : RF.Raise-406-Exception","detail":{"errorcode":"steps.raisefault.RaiseFault"}}}
让我感到困惑的是 406 代码,因为我已将内容类型和接受标头都设置为“应用程序/json”,这是服务所需要的。 但是,第二条消息说“无法生成 OAuth2 令牌”,那么它可能是获取返回 406 的 oauth 令牌的调用吗?如果是这样,我如何在令牌服务调用中设置接受标头?
使用 Ballerina,我调用了令牌端点并成功获得了令牌,但如果我尝试使用 PasswordGrantConfig 调用服务,则会出现这些错误。我已经尝试了我能想到的一切,并成功地使用 ClientCredentialsGrantConfig 让其他服务正常工作。 感谢您提供任何帮助。
相关代码如下。以下三个部分是 3 个不同 .bal 文件中的代码部分。
// configure the Oauth2 Config
import ballerina/config;
import ballerina/http;
import ballerina/oauth2;
public function getOauth2Handler() returns http:BearerAuthHandler {
oauth2:PasswordGrantConfig passwordGrantConfig = {
tokenUrl: config:getAsString("experian.authentication.tokenUrl"),
username: config:getAsString("experian.authentication.username"),
password: config:getAsString("experian.authentication.password"),
clientId: config:getAsString("experian.authentication.clientId"),
clientSecret: config:getAsString("experian.authentication.clientSecret"),
credentialBearer: http:AUTH_HEADER_BEARER
};
oauth2:OutboundOAuth2Provider oauth2Provider = new (passwordGrantConfig);
return new (oauth2Provider);
}
// Configure the API Client
http:ClientConfiguration delphiSelectClientConfig = {
auth: {
authHandler: experian:getOauth2Handler()
}
};
experian:DelphiSelectClientConfig delphiSelectConfig = {
serviceUrl: config:getAsString("experian.services.delphi-select.serviceUrl"),
clientConfig: delphiSelectClientConfig
};
experian:DelphiSelectClient delphiSelectClient = new (delphiSelectConfig);
// Call the endpoint using the Oath2 configuration
import ballerina/http;
import ballerina/io;
public type DelphiSelectClientConfig record {
string serviceUrl;
http:ClientConfiguration clientConfig;
};
//==============================
//============Client============
//==============================
public type DelphiSelectClient client object {
public http:Client clientEp;
public http:ClientConfiguration config;
public function __init(DelphiSelectClientConfig config) {
http:Client httpEp = new (config.serviceUrl, {auth: config.clientConfig.auth});
self.clientEp = httpEp;
self.config = config.clientConfig;
}
public remote function newApplication() returns @untainted json|error {
io:println("In newApplication function");
http:Request request = new;
json requestBody = newApplicationBody; // get test data from json in another file
request.setJsonPayload(requestBody);
var response = check self.clientEp->post("/application", request);
var payload = check response.getJsonPayload();
return payload;
}
};
我还修改了我的测试代码以调用令牌 EP 并故意将accept 设置为不可接受的值,例如"text/csv"。在这种情况下,我得到相同的错误响应。但是将accept 设置为"*/*" 确实有效。最后一个考试; accept 的 ""(空)也失败了,所以我怀疑 BearerAuthHandler 没有为 accept 设置任何值。
那么我可以强制 BearerAuthHandler 设置 accept 的 "application/json" 吗?
此外,您引用的 Oath2 规范中的示例显示了正在设置的内容类型值。即使是 “*/*” 的值也可以,但我怀疑 Ballerina 将其留空。
我已经提出了 GitHub 问题 Need to be able to set http header values for OutboundOAuth2Provider
【问题讨论】:
-
您调用的授权端点不接受您的请求。这就是你得到 406 的原因。你能分享你的代码没有凭据吗?
-
请参阅How to create a Minimal, Reproducible Example。不查看导致问题的代码就无法解决问题。