简体   繁体   中英

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)

To be specific II don't know how should my headers look like.

Calling API using Postman with authorization works. My API is hosted on localhost:44320 I didn't deploy it.

In React I use 'react-azure-adb2c' library and it also works. 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 .

So Authorization: 'Bearer ' + token in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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