邮递员获取令牌的预请求不起作用

时间:2020-05-30 13:26:43

标签: javascript spring-security postman urlencode

这时,我可以成功执行我的邮递员请求以获取我的令牌。我正在使用这些参数:

-标题中的基本授权

-和这个身体 -enter image description here

现在,我希望将此请求作为请求前脚本获得(并使用环境变量作为令牌)。

这是脚本:

pm.sendRequest({
    url: 'http://localhost:8084/oauth/token',
    method: 'POST',
    header: {
        'Authorization':'Basic Y2xpZW50OnBhc3N3b3Jk',
        'content-type': 'application/x-www-form-urlencoded'
    },
    data:{
        'password':'secret',
        'username':'admin',
        'grant_type':'password'
    }
}, (err, res) => pm.environment.set("token", res.json().access_token));

它与响应不起作用:Full authentication is required to access this resource

怎么了?

谢谢

1 个答案:

答案 0 :(得分:2)

您可以将data部分更改为类似的内容吗?

body:{
   mode:"urlencoded",
   urlencoded:[
      {
         key:"grant_type",
         value:"password"
      },
      {
         key:"username",
         value:"admin"
      },
      {
         key:"password",
         value:"secret"
      }
   ]
}

here可以找到pm.sendRequest()个示例的丰富资源