Microsoft Powerbi React访问令牌错误no no' Access-Control-Allow-Origin'标题存在于请求的资源上

发布于 2025-02-13 12:58:58 字数 1404 浏览 0 评论 0原文

我正在尝试在我的React应用程序中获取Microsoft PowerBi访问令牌,并且显示错误时,当我使用fetch时,请求的资源上没有“访问控制”标头。您可以找到我在下面尝试的内容: -

useEffect(() => {
      let accessTokenData = {
        'grant_type':'password',
        'username':'******',
        'password':'******',
        'client_id': '**********',
        'client_secret': '********',
        'resource': 'https://analysis.windows.net/powerbi/api'
      }
      let formbody = [];
      for (let property in accessTokenData) {
        let encodedKey = encodeURIComponent(property);
        let encodedValue = encodeURIComponent(accessTokenData[property]);
        formbody.push(encodedKey + "=" + encodedValue);
      }
      formbody = formbody.join("&");

      fetch(`https://login.microsoftonline.com/common/oauth2/token`, {
      // mode: 'no-cors',
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      },
      body: formbody
      })
      .then( data => data.json())
      .then(
      data => {
          setAccessToken(data)
          console.log(data['access_token'])
      }
      )
      .catch( error => console.error(error))
  }, []);

加载页面时显示以下错误

如果您经历了处理此问题的经历或从事类似的事情,我要求您指导我这一问题。

I am trying to get microsoft powerbi access token in my react app and it is showing error No 'Access-Control-Allow-Origin' header is present on the requested resource when I use fetch. You can find what I tried below :-

useEffect(() => {
      let accessTokenData = {
        'grant_type':'password',
        'username':'******',
        'password':'******',
        'client_id': '**********',
        'client_secret': '********',
        'resource': 'https://analysis.windows.net/powerbi/api'
      }
      let formbody = [];
      for (let property in accessTokenData) {
        let encodedKey = encodeURIComponent(property);
        let encodedValue = encodeURIComponent(accessTokenData[property]);
        formbody.push(encodedKey + "=" + encodedValue);
      }
      formbody = formbody.join("&");

      fetch(`https://login.microsoftonline.com/common/oauth2/token`, {
      // mode: 'no-cors',
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
      },
      body: formbody
      })
      .then( data => data.json())
      .then(
      data => {
          setAccessToken(data)
          console.log(data['access_token'])
      }
      )
      .catch( error => console.error(error))
  }, []);

On loading the page it is showing the following error
CORS Issue

If you have experienced dealing with this issue or worked on something similar I request you to please guide me in this regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2025-02-20 12:59:02

此错误是由于CORS造成的,请在thread -github.com/jaredhanson/passport/issues/582上检查类似问题

This error is due to cors, check the similar issue on thread - github.com/jaredhanson/passport/issues/582

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文