繁体   English   中英

带有 Azure AD B2C 授权的 API 的 fetch() 方法示例

[英]Example of fetch() method to API with Azure AD B2C authorization

我正在寻找调用安全 API 的 fetch() 方法的示例(我使用 Azure AD B2C)

具体来说,我不知道我的标题应该是什么样子。

使用 Postman 调用 API 并获得授权。 我的 API 托管在 localhost:44320 我没有部署它。

在 React 中,我使用“react-azure-adb2c”库,它也可以工作。 我可以登录,之后我将获得包含我需要的所有声明的令牌。

var token = auth.getToken(); //here is the token which is correct
fetch("https://localhost:44320/api/worker/", {
      method: "GET",
      headers: {
        Authorization: token,
        Accept: "application/json",
        Host: "localhost:44320"
      }
    })
      .then(res => res.json())
      .then(json => this.setState({ listOfWorkers: json.results }));
  }

您将 header 指定为Authorization: Bearer token-value-here

所以Authorization: 'Bearer ' + token在你的情况下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM