首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ballerina Oauth2经过身份验证的终结点返回406

Ballerina Oauth2经过身份验证的终结点返回406
EN

Stack Overflow用户
提问于 2020-04-24 00:09:22
回答 1查看 319关注 0票数 1

我正在尝试调用使用Oauth2密码凭据来获取身份验证令牌的第三方服务。芭蕾舞女演员正在回复以下消息。

代码语言:javascript
复制
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代码让我感到困惑,因为我已经将内容类型和accept标头都设置为"application/json“,这正是服务所需的。然而,第二条消息显示“未能生成OAuth令牌”,那么会不会是获取OAuth2令牌的调用返回了406值呢?如果是这样,我如何在令牌服务调用上设置accept标头?

使用Ballerina,我已经调用了令牌端点并成功地获得了令牌,但是如果我尝试使用PasswordGrantConfig调用服务,这些就是我得到的错误。我已经尝试了我能想到的所有方法,并成功地让其他使用ClientCredentialsGrantConfig的服务正常工作。感谢您给予的任何帮助。

相关代码如下。下面的三个部分是三个不同.bal文件中的代码部分。

代码语言:javascript
复制
// 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

EN

回答 1

Stack Overflow用户

发布于 2020-04-24 20:26:00

http:OutboundAuthHandler对象的主要目的是准备带有身份验证信息的http:Request,这些信息需要通过您调用的外部端点进行身份验证。

http:BearerAuthHandler负责添加具有Bearer <token>值的Authorization报头。"token“是用提供的信息准备的。因此,没有强制http:BearerAuthHandler为请求设置任何标头的选项。

但在这种情况下,如果接口成功响应,且存在值为application/jsonAccept header,则只需在调用POST请求之前将该header添加到http:Request中,如下所示:

request.addHeader("Accept", "application/json");

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61391843

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档