[npm start得到401,节点app.js得到200响应

问题描述 投票:0回答:2

我有一个使用npm start运行的React项目,此代码从第二次读取中获得401错误(第一个正常)。它仅在节点返回200时运行良好,例如在“ node App.js”中。

所以我需要做什么来运行我的React项目以得到200个响应?为什么npm和此请求响应的节点之间存在这种差异?

const clientID = <ClientID>
const clientSecret = <ClientSecret>
const encode = Buffer.from(`${clientID}:${clientSecret}`, 'utf8').toString('base64')

const requestOptions = {
    method: 'POST',
    headers: {  'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': `Basic ${encode}`,
            },
};

fetch("https://auth-nato.auth.us-east-1.amazoncognito.com/oauth2/token?grant_type=client_credentials", requestOptions)
.then(response => { return response.json() })
.then(data => { 
  const requestOptions2 = {
    method: 'POST',
    mode: 'no-cors',
    headers: {  'Content-Type': 'application/json',
                'Authorization': `Bearer ${data.access_token}`
            },
    body: '{"username":"Ana", "password":"test123","user_id":"[email protected]"}'
  };
  fetch('https://j1r07lanr6.execute-api.sa-east-1.amazonaws.com/v1/register', requestOptions2)
  .then(response => {console.log(response)});
})
javascript node.js amazon-web-services fetch-api http-status-code-401
2个回答
0
投票

Buffer-在浏览器的JavaScript中不显示。

代替

const encode = Buffer.from(`${clientID}:${clientSecret}`, 'utf8').toString('base64')

仅使用

const encode = btoa(`${clientID}:${clientSecret}`);

阅读有关MDN上base64编码的更多信息。


0
投票

唯一的解决方案是禁用chrome web安全性并删除“模式:no-cors”。我尝试将“ Access-Control-Allow-Origin”:“ http://localhost:3000”添加到标题中,但不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.