0

I'm unable to get my custom web part to connect to my custom Web API.

Ultimately, my goal is to secure the API using Azure AD and then call that API from my web part.

In the local workbench, I'm getting the following error:

ERROR: To view the information on this page, ask a global or SharePoint admin in your organization to go to the API management page in the new SharePoint admin center.

aadHttpClient error

If I try it in the hosted workbench I get the same message but a 405 Method not allowed error appears in the console.

Here's my code:

this.context.aadHttpClientFactory
  .getClient('https://www.mysite.com/MyWebAPI')
  .then((client: AadHttpClient): void => {
    client
      .get('https://www.mysite.com/MyWebAPI/api/apps', AadHttpClient.configurations.v1)
      .then((response: HttpClientResponse): Promise<any> => {
        return response.json();
      })
      .then((data: any): void => {
        // process the data            
      }, (err: any): void => {
        this.context.statusRenderer.renderError(this.domElement, err);
      });
  }, (err: any): void => {
    this.context.statusRenderer.renderError(this.domElement, err);
  });

If I choose not to use the aadHttpClient and just do a normal get() and then remove the <Authorize> from my web API, I can connect without issue but the second I start trying to secure the API, I lose the ability to connect from the custom web part.

How do I fix this issue?

1
  • when you call ``` this.context.aadHttpClientFactory .getClient('mysite.com/MyWebAPI') ``` I assume that is to generate the token, but where is this api? I have an inhouse API secured via Azure and requires a token from Azure, this has a registration application in Portal Azure, and it will be invoked in ``` .get('mysite.com/MyWebAPI/api/apps', AadHttpClient.configurations.v1) ``` This is the API inhouse, no? But the previous API you call? I don´t understand where it came from. Thank you
    – Bire00
    Nov 17, 2022 at 14:53

1 Answer 1

0

Ultimately, my issue was my web API. I'm not sure what was wrong, since I believe I followed the tutorial well but I ended up taking one of the SharePoint sample web APIs done in ASP.NET Core (whereas mine was a traditional .NET Web API) and copy over my models and controllers and I was able to connect without issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.