SharePoint / sp-dev-docs

SharePoint & Viva Connections Developer Documentation
https://docs.microsoft.com/en-us/sharepoint/dev/
Creative Commons Attribution 4.0 International
1.25k stars 1.02k forks source link

SPFx webparts not able to acquire token in SharePoint #9972

Open svrijsewijk opened 1 month ago

svrijsewijk commented 1 month ago

Target SharePoint environment

SharePoint Online

What SharePoint development model, framework, SDK or API is this about?

πŸ’₯ SharePoint Framework

Developer environment

None

What browser(s) / client(s) have you tested

Additional environment details

Describe the bug / error

We have created a Webpart for SharePoint. This webpart loads a button in SharePoint document collections when items are selected. Pressing the button calls our own separate API. A token is included in the header of the request.

Before the request is initiated a token is fetched.

const tokenProvider = await this.context.aadTokenProviderFactory.getTokenProvider();
const authToken = await this.tokenProvider.getToken(config.authAppId);

This produces a GET request that fails with error 500

https://testinfoprojects.sharepoint.com/sites/dev-testing/_api/Microsoft.SharePoint.Internal.ClientSideComponent.Token.AcquireOBOToken?resource='api://iprox******'&clientId='ac5c3377-6028-42d9-a545-07699813e16d'

Error response:

{
  "odata.error": {
    "code": "-1, System.AggregateException",
    "message": { "lang": "en-US", "value": "One or more errors occurred." }
  }
}

where api://iprox** is an app registration in our tenant.

I've found similar issues: https://techcommunity.microsoft.com/t5/sharepoint-developer/spfx-webparts-not-able-to-acquire-token-for-graph-api-in-teams/m-p/3886488 which leads to: https://github.com/SharePoint/sp-dev-docs/issues/9099#issuecomment-1701406090

and this one: https://github.com/SharePoint/sp-dev-docs/issues/4915

Neither proposed solutions by others have any effect on the error.

We're kind of lost here at the moment.

Steps to reproduce

1. 2. 3.

Expected behavior

A 200 status code after which the webpart can use the token in its headers

esushilnikov commented 2 weeks ago

I have the same issue.

My code:

return await this.context.aadHttpClientFactory
            .getClient(this.resourceEndpoint)
            .then((client: AadHttpClient) =>
                client
                    .post(this.externalApiAuthUrl, AadHttpClient.configurations.v1, httpClientOptions)
                    .catch(r => { //  "One or more errors occurred."
                        console.log(r);
                    })
                    .then((response: HttpClientResponse): Promise<string> => response.json())
            ).then(token => token);
esushilnikov commented 2 weeks ago

@svrijsewijk , Please check this answer, it helps for me - https://github.com/SharePoint/sp-dev-docs/issues/9099#issuecomment-1701406090