簡體   English   中英

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

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

我目前正在寫入 API 以嘗試獲取令牌。 我快到了,但在最后一個障礙中倒下了..

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(); 
    }
)

編碼的令牌在第一部分恢復正常,但 https 請求返回了一個問題。

我得到的回復是 grant_type 丟失,所以我知道由於這個 x-www-form-urlencoded 我有格式問題,但我不知道如何解決它。

這是網站上說的:

您需要在請求正文中以 x-www-form-urlencoded 格式包含以下數據:

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
&client_assertion="
終點

理想情況下,我想要一個使用 https 請求的解決方案,但如果不可能,我願意接受其他解決方案。

任何幫助是極大的贊賞。

謝謝,克雷格

編輯 - 我根據以下建議更新了我的代碼:

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);
})

但我仍然收到錯誤代碼 400,但現在關於原因的細節較少。 (錯誤代碼 400 有多個消息失敗)

郵遞員是最好的。

感謝@Anatoly 的支持,這有助於為我指明正確的方向。 我運氣不好,所以第一次使用 postman,發現它有一個代碼片段部分,有四種使用 node.js 實現這一點的不同方法。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM