NodeJS :类型错误:无法反结构“undefined”或“null”的属性“access_token”

r3i60tvu  于 2022-12-18  发布在  Node.js
关注(0)|答案(2)|浏览(140)

我目前正在从Zoom API获取一个令牌,它工作正常。问题在于之后,当我试图解构属性以便获得access_token时。

var username = "client id";
  var password = "client password";

var options = {
  method: 'POST',
  url: 'https://zoom.us/oauth/token?grant_type=client_credentials',
  headers: {
    /**The credential below is a sample base64 encoded credential. Replace it with "Authorization: 'Basic ' + Buffer.from(your_app_client_id + ':' + your_app_client_secret).toString('base64')"
    **/
   Authorization: 'Basic ' + Buffer.from(username + ':' + password).toString('base64'),
    "Content-Type": "application/x-www-form-urlencoded"
    }
  };

const data = {
 "topic": "Demo Meeting",
             "type": 2,
             "start_time": "2020-05-05 12:00:00",
             "password": "Hey123",
             "agenda": "This is the meeting description",
             "settings": {
             "host_video": false,
             "participant_video": false,
             "join_before_host": false,
             "mute_upon_entry": true,
             "use_pmi": false,
             "approval_type": 0
             }
};


function getResponse() {
  return rp(options, function(error, response, body) {
   if (error) throw new Error(error)   

    const { access_token } = response.data;
    console.log(access_token)
    
  }).then(function (req) {
      //logic for your response
      console.log("inside response", req);
    
   return fetch("https://api.zoom.us/v2/users/me/meetings", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${req}`,
        "Content-Type": "application/json"
      },
      body: JSON.stringify(data)
  });
    })
    .catch(function (err) {
        // API call failed...
        console.log('API call failed, reason ', err);
    });
}

当我运行代码时,我得到了以下错误:类型错误:无法破坏“未定义”或“null”的属性access_token的结构。
为什么会这样?

vawmfj5a

vawmfj5a1#

错误消息表示response.dataundefined
console.log(response)放入代码中,查看其中的内容,您会发现缺少data属性。

v440hwme

v440hwme2#

我遇到了同样的问题,我通过以下操作解决了它:
创建一个名为AccessToken的参数,设置为req.oidc.accessToken,在调用const { token_type,access_token } = req.oidc.accessToken之前创建;

router.get('/', (req, res) => {
res.render('index', {
    title: "Express Demo",
    isAuthenticated: req.oidc.isAuthenticated(),
    user: req.oidc.user,
    accessToken: req.oidc.accessToken, // this part, when render create accessToken parameter     })

})

相关问题