Postman中OAuth2.0访问令牌的自动递增ID

k75qkfdt  于 2023-11-16  发布在  Postman
关注(0)|答案(1)|浏览(159)

我正在尝试实现一个自动递增的ID来附加到我的令牌名称,以便更好地区分在不同时间检索的令牌。
我可以使用以下代码创建一个名为tokenCount的变量,并在tests选项卡中自动递增它:

let tokenCount = pm.collectionVariables.get("tokenCount");
tokenCount++;
pm.collectionVariables.set("tokenCount", tokenCount);

字符串
但是,这只在我发送请求时增加,而不是在我检索新的访问令牌时。有没有办法可以在检索访问令牌时运行此代码?

nle07wnf

nle07wnf1#

以下是使用OAuth 2.0针对Microsoft进行身份验证的预请求脚本的示例。
您需要更改主体以匹配您正在使用的授权类型。

let currentDateTime = Date.now();
let tokenExpiry = pm.environment.get("bearerTokenExpiresOn")
// console.log("currentDateTime: " + currentDateTime);
// console.log("tokenExpiry: " + tokenExpiry);
if (!pm.environment.get("bearerToken") || currentDateTime > tokenExpiry) {
    pm.test("Pre-request check for Environment Variables", function () {
        let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
        vars.forEach(function (item) {
            // console.log(item);
            pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.undefined;
            pm.expect(pm.environment.get(item), item + " variable not set").to.not.be.empty;
        });
        pm.sendRequest({
            url: 'https://login.microsoftonline.com/' + pm.environment.get("tenantId") + '/oauth2/v2.0/token',
            method: 'POST',
            header: 'Content-Type: application/x-www-form-urlencoded',
            body: {
                mode: 'urlencoded',
                urlencoded: [
                    { key: "client_id", value: pm.environment.get("clientId"), disabled: false },
                    { key: "scope", value: pm.environment.get("scope"), disabled: false },
                    { key: "username", value: pm.environment.get("username"), disabled: false },
                    { key: "password", value: pm.environment.get("password"), disabled: false },
                    { key: "client_secret", value: pm.environment.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
                ]
            }
        }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                pm.test("Pre-request Microsoft login Status code is 200", () => {
                    pm.expect(res).to.have.status(200);
                    let resJson = res.json();
                    // console.log(resJson);
                    let tokenName = pm.environment.get("tokenName")
                    let tokenCount = pm.environment.get("tokenCount");
                    tokenCount++;
                    pm.environment.set(tokenName + tokenCount, resJson.id_token);
                    pm.environment.set("bearerTokenExpiresOn", Date.now() + resJson.expires_in * 1000);
                    pm.environment.set("tokenCount", tokenCount);
                    // console.log("bearerTokenExpiresOn: " + pm.environment.get("bearerTokenExpiresOn"));
                });
            }
        });
    });
};

字符串
这些是处理自动递增数字的添加行。

let tokenName = pm.environment.get("tokenName")
let tokenCount = pm.environment.get("tokenCount");
tokenCount++;
pm.environment.set(tokenName + tokenCount, resJson.id_token);
pm.environment.set("tokenCount", tokenCount);


你需要使用环境变量tokenName和tokenCount。你还需要初始化tokenCount。例如000。

相关问题