Twitter API 获取请求返回 CORS 错误

Twitter API Fetch Request returning CORS error

所以我一直在尝试在我一直在开发的 React 应用程序中使用 Twitter AP https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/quick-start。我正在使用 fetch API 来简单地点击快速入门教程中提到的端点。

每次我尝试在浏览器中访问端点时,我都会收到以下错误:

Access to fetch at 'https://api.twitter.com/2/users/by?user.fields=&usernames=trengriffin,awealthofcs,ReformedBroker,michaelbatnick,Wu_Tang_Finance&tweet.fields=author_id,created_at' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' when the request's credentials mode is 'include'.

但是,当我用与邮递员相同的参数点击这个 URL 时,我正确地取回了数据。我想这一定是我使用 fetch API 的方式,但我没有在网上看到任何示例来解决这个问题。

代码如下:

const endpointUrl = 'https://api.twitter.com/2/users/by?user.fields=&usernames=trengriffin,awealthofcs,ReformedBroker,michaelbatnick,Wu_Tang_Finance&tweet.fields=author_id,created_at';
const TwitterContainer: React.FC<ContainerProps> = () => {
   useEffect( () => {
      fetch(endpointUrl, {
         mode: 'cors',
         credentials: 'include',
         headers: {
            "Authorization": `Bearer ${token}`
         },

      }).then(res => res.json())
          .then(console.log)
          .catch(console.error);

我不确定为什么我仍然收到错误消息。根据 fetch 文档,您必须使用 credentials: include 进行 HTTPS 请求,但我仍然遇到 CORS 问题。

推特 API 不支持 CORS。有关详细信息,请参阅此问题: