When calling a Sharepoint REST API, which token do I need to pass and in which header?
Currently I am doing:
const result = await fetch(`${sharepointUrl}/_api/search/query?querytext='${searchtext}'`, {
headers: {
Bearer: token,
Accept: 'application/json;odata=verbose',
'odata-version': '',
},
})
Where token
is the value that was returned by the OAuth login. I am using NextAuth with the azure-ad provider (https://next-auth.js.org/providers/azure-ad). Which works fine and the users can login without any problem using their Azure AD/Entra ID account. This gives me these fields - screenshot below. I am using next_auth_account.access_token
as the bearer token. I have also tried next_auth_session.sessionToken
However I am getting 403 Forbidden
as the result. So, am I using the wrong header field name? The wrong field to get the token from? The wrong something else? I am logging in as myself to test, and I do have access to the Sharepoint that I am searching.

And I have setup the Sites.ReadWrite.All permission in Azure:

The response from Sharepoint is:
Response {
[Symbol(realm)]: null,
[Symbol(state)]: {
aborted: false,
rangeRequested: false,
timingAllowPassed: true,
requestIncludesCredentials: true,
type: 'default',
status: 403,
timingInfo: [Object],
cacheState: '',
statusText: 'Forbidden',
headersList: [HeadersList],
urlList: [Array],
body: [Object]
},
[Symbol(headers)]: HeadersList {
cookies: null,
[Symbol(headers map)]: [Map],
[Symbol(headers map sorted)]: null
}
}