简体   繁体   English

x-www-form-urlencoded 格式 - 在 node.js 中使用 https

[英]x-www-form-urlencoded format - using https in node.js

I am currently writing to an API to try and get a token.我目前正在写入 API 以尝试获取令牌。 I'm nearly there but fallen at the last hurdle..我快到了,但在最后一个障碍中倒下了..

const fs = require('fs');
const https = require('https');
const ConfigParams = JSON.parse(fs.readFileSync('Config.json', 'utf8'));
const jwt = require('jsonwebtoken');
const apikey = ConfigParams.client_id;


var privateKey = fs.readFileSync(**MY KEY**);
var tkn;

const jwtOptions = {
    algorithm: 'RS512',
    header: { kid: 'test-1' }
}


const jwtPayload = {
    iss: apikey,
    sub: apikey,
    aud: **API TOKEN ENDPOINT**,
    jti: '1',
    exp: 300
}

jwt.sign(jwtPayload,
    privateKey,
    jwtOptions,
    (err, token) => {
        console.log(err);
        //console.log(token);
        tkn = token;

        let = tokenPayload = {
            grant_type: 'client_credentials',
            client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer/',
            client_assertion: tkn
        }

        tokenAuthOptions = {
            payload: tokenPayload,
            host: **HOST**,
            path: **PATH**,
            method: 'POST',
            

        }
          
        https.request(
            tokenAuthOptions,
            resp => {
                var body = '';
                resp.on('data', function (chunk) {
                    body += chunk;
                });
                resp.on('end', function () {
                    console.log(body);
                    console.log(resp.statusCode);
                });
            }
        ).end(); 
    }
)

the encoded token comes back fine for the first part, the https request though returns a problem.编码的令牌在第一部分恢复正常,但 https 请求返回了一个问题。

the response I get back is grant_type is missing, so I know I have a formatting problem due to this x-www-form-urlencoded, but I can't figure out how to fix it.我得到的回复是 grant_type 丢失,所以我知道由于这个 x-www-form-urlencoded 我有格式问题,但我不知道如何解决它。

here is what the website said:这是网站上说的:

You need to include the following data in the request body in x-www-form-urlencoded format:您需要在请求正文中以 x-www-form-urlencoded 格式包含以下数据:

grant_type = client_credentials client_assertion_type = urn:ietf:params:oauth:client-assertion-type:jwt-bearer client_assertion = <your signed JWT from step 4> Here's a complete example, as a CURL command: grant_type = client_credentials client_assertion_type = urn:ietf:params:oauth:client-assertion-type:jwt-bearer client_assertion = <您在步骤 4 中签名的 JWT> 这是一个完整的示例,作为 CURL 命令:

curl -X POST -H "content-type:application/x-www-form-urlencoded" --data \\ "grant_type=client_credentials\\ &client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer curl -X POST -H "content-type:application/x-www-form-urlencoded" --data \\ "grant_type=client_credentials\\ &client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
&client_assertion=" &client_assertion="
END POINT终点

Ideally I want a solution using the https request, but if that's not possible I'm open to other solutions.理想情况下,我想要一个使用 https 请求的解决方案,但如果不可能,我愿意接受其他解决方案。

Any help is greatly appreciated.任何帮助是极大的赞赏。

Thanks, Craig谢谢,克雷格

Edit - I updated my code based on a suggestion to:编辑 - 我根据以下建议更新了我的代码:

const params = new url.URLSearchParams({
    grant_type: 'client_credentials',
    client_assertion_type: 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer/',
    client_assertion: tkn
});

axios.post("URL", params.toString()).then(resp => {
    console.log("response was : " + resp.status);
}).catch(err => {
    console.log("there was an error: " + err);
})

But I'm still getting an error code 400, but now with less detail as to why.但我仍然收到错误代码 400,但现在关于原因的细节较少。 (error code 400 has multiple message failures) (错误代码 400 有多个消息失败)

Postman is the best.邮递员是最好的。

Thank for @Anatoly for your support which helped to point me in the right direction.感谢@Anatoly 的支持,这有助于为我指明正确的方向。 I had no luck so used postman for the first time, and found it had a code snippet section, with four different ways of achieving this using node.js.我运气不好,所以第一次使用 postman,发现它有一个代码片段部分,有四种使用 node.js 实现这一点的不同方法。

The solution with Axion was: Axion 的解决方案是:

const axios = require('axios').default;
const qs = require('qs');

        var data = qs.stringify({
            'grant_type': 'client_credentials',
            'client_assertion_type': 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer',
            'client_assertion': tkn
        });
        var config = {
            method: 'post',
            url: '',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: data
        };

        axios(config)
            .then(function (response) {
                console.log(JSON.stringify(response.status));
            })
            .catch(function (error) {
                console.log(error);
            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有 application/x-www-form-urlencoded 格式的 Node.js Axios POST 请求? - Node.js Axios POST request with application/x-www-form-urlencoded format? 使用application / x-www-form-urlencoded使用node.js在发布请求中发送数组 - Send Array in post request using node.js using application/x-www-form-urlencoded Node.js:Axios 与使用 x-www-form-urlencoded 获取 - Node.js: Axios vs Fetch using x-www-form-urlencoded 如何在 node.js 中发布内容类型 =&#39;application/x-www-form-urlencoded&#39; 的数据 - how to post data in node.js with content type ='application/x-www-form-urlencoded' node.js正文解析器对Content-Type:x-www-form-urlencoded和Form-data JSON的错误解释 - node.js body-parser bad interpretation of Content-Type:x-www-form-urlencoded and Form-data JSON 如何在 Node 服务器上读取以 application/x-www-form-urlencoded 格式接收的数据? - How can I read the data received in application/x-www-form-urlencoded format on Node server? node js 如何通过发送数据调用webapi( x-www-form-urlencoded ) - node js how to call webapi( x-www-form-urlencoded ) by sending data 如何在节点js中使用参数为application / x-www-form-urlencoded类型的POST调用 - How to consume POST call where parameter is of type application/x-www-form-urlencoded in node js 如何使用节点请求发送承载令牌和x-www-form-urlencoded数据 - How to send bearer token and x-www-form-urlencoded data using Node Request 节点js如何识别邮递员扩展客户端请求来自“表单数据”或“ x-www-form-urlencoded” - node js how to identify postman extention client request came from “form-data” or “x-www-form-urlencoded”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM