在postman中将变量从一个请求传递到另一个请求时出错

h43kikqp  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(169)

我有以下代码,希望将访问令牌值从一个请求传递到另一个请求

let tokenUrl = 'https://login.microsoft.com/<tenantid>/oauth2/v2.0/token';
const getTokenRequest = {
method: 'POST',
url: tokenUrl,
body: {
mode: 'formdata',
formdata: [
    {'key':'client_id', 'value':'<client_id>'},
    {'key':'client_secret', 'value':'<client_secret>'},
    {'key':'scope', 'value':'<api_scope>'},
    {'key':'grant_type', 'value':'password'},
    {'key':'username', 'value':'<usr_name>'},
    {'key':'password', 'value':'<password>'}
]
},
};
pm.sendRequest(getTokenRequest, (err, response) => {
const jsonResponse = response.json();
const newAccessToken = jsonResponse.access_token;
//console.log("------->",newAccessToken);
pm.variables.set('GRAPHTOKEN', newAccessToken);
pm.environment.set("GRAPHTOKEN", "newAccessToken");
pm.globals.set("GRAPHTOKEN", "newAccessToken");
if (err){
    console.log(err)
    }
});
let createGroupIDURL = "https://localhost/api/groups";
const getGroupIDRequest = {
'method': 'POST',
'url': createGroupIDURL,
'body': `{"displayName": "MI-GroupInv-GRP-PERFweNf","description": "MI-GroupInv-GRP-PERFweNf"}`,
'headers': {
  'Content-Type': `application/json;odata.metadata=minimal;odata.streaming=true`,
  'Authorization': `Bearer {{newAccessToken}}`
}
};
pm.sendRequest(getGroupIDRequest, (err, response) => {
const jsonResponseBody = response.json();
const groupd = jsonResponseBody.id;
console.log("------->",groupd);
if (err){
    console.log(err)
    }
});

我遇到的错误:不记名令牌没有正确传递任何线索如何修复或我做错了什么?

yhived7q

yhived7q1#

发现您需要修复代码中的几行,

let tokenUrl = 'https://login.microsoft.com/<tenantid>/oauth2/v2.0/token';
const getTokenRequest = {
   method: 'POST',
   url: tokenUrl,
   body: {
      mode: 'formdata',
      formdata: [{
            'key': 'client_id',
            'value': '<client_id>'
         },
         {
            'key': 'client_secret',
            'value': '<client_secret>'
         },
         {
            'key': 'scope',
            'value': '<api_scope>'
         },
         {
            'key': 'grant_type',
            'value': 'password'
         },
         {
            'key': 'username',
            'value': '<usr_name>'
         },
         {
            'key': 'password',
            'value': '<password>'
         }
      ]
   },
};
pm.sendRequest(getTokenRequest, (err, response) => {
   const jsonResponse = response.json();
   const newAccessToken = jsonResponse.access_token;
   pm.variables.set("GRAPHTOKEN-VAR", newAccessToken);  //Local Variable
   pm.environment.set("GRAPHTOKEN-ENV", newAccessToken); //Environment Variable
   pm.globals.set("GRAPHTOKEN-GLB", newAccessToken);  //Gloabal Variable
   if (err) {
      console.log(err)
   }
});

pm.sendRequest({
   url: "https://localhost/api/groups",
   method: 'POST',
   header: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${pm.globals.get('GRAPHTOKEN-GLB')}`  // access the global variable
   },
   body: {    //also sending the JSON payload properly
      mode: 'raw',
      raw: JSON.stringify({
         "displayName": "MI-GroupInv-GRP-PERFweNf",
         "description": "MI-GroupInv-GRP-PERFweNf"
      })
   }
}, function (err, jsonResponseBody ) {
   const jsonResponse  = jsonResponseBody .json();
   const iD = jsonResponse.id;
   console.log("ID:",iD)
   if (err) {
      console.log(err)
   }
});

相关问题