简体   繁体   English

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

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

I'm looking for example of fetch() method to call secured API (I use Azure AD B2C)我正在寻找调用安全 API 的 fetch() 方法的示例(我使用 Azure AD B2C)

To be specific II don't know how should my headers look like.具体来说,我不知道我的标题应该是什么样子。

Calling API using Postman with authorization works.使用 Postman 调用 API 并获得授权。 My API is hosted on localhost:44320 I didn't deploy it.我的 API 托管在 localhost:44320 我没有部署它。

In React I use 'react-azure-adb2c' library and it also works.在 React 中,我使用“react-azure-adb2c”库,它也可以工作。 I can log in and after that I'm getting token with all claims which I need.我可以登录,之后我将获得包含我需要的所有声明的令牌。

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 }));
  }

You specify the header as Authorization: Bearer token-value-here .您将 header 指定为Authorization: Bearer token-value-here

So Authorization: 'Bearer ' + token in your case.所以Authorization: 'Bearer ' + token在你的情况下。

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

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