【问题标题】:Postman access refresh_token?邮递员访问refresh_token?
【发布时间】:2022-10-01 00:14:32
【问题描述】:

我将 Postman 配置为使用授权代码流。问题是我们的令牌到期很快,每次到期时我都需要重新运行流程。所以我想在预请求脚本中实现一个 refresh_token 流程(除非有一个 Postman-native 方式来做到这一点)。

现在我的问题是在哪里可以找到refresh_token?有什么方法可以访问它还是“扔掉”而只使用access_token

    标签: postman postman-pre-request-script


    【解决方案1】:

    将以下代码添加到 Collection Pre-request Script。 (将其编辑为您自己的网址和正文。)

    // Set refresh and access tokens
    const loginRequest = {
        url: pm.environment.get("mainUrl") + "/authenticate/login",  //The url that the token returns
        method: 'POST',
        header: {
            'content-type': 'application/json',
            'Accept': "*/*"
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify({    //Your body
                "username": pm.environment.get("username"),
                "password": pm.environment.get("password")
            })
        }
    };
    
    pm.sendRequest(loginRequest, function (err, res) {
        pm.environment.set("accessToken", res.json().accessToken); //The token returned in the response and the environment value to which the value will be sent
        pm.environment.set("refreshToken", res.json().refreshToken);
    });
    

    此请求在每个请求之前运行。

    最后,在请求的“授权”选项卡的“令牌”字段中,从环境中调用 accessToken 值。

    {{accessToken}}

    每次请求运行时,都会刷新令牌值并使用该值。

    【讨论】:

      猜你喜欢
      • 2016-12-04
      • 1970-01-01
      • 2018-08-14
      • 2020-07-13
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 2020-05-23
      • 2018-01-26
      相关资源
      最近更新 更多