0

How do you allow an external application to use the search REST API? This is for Sharepoint online, not self hosted. Both SP and the external app use the same Azure AD/MS Graph to login. So the users are the same, and have access to both.

I get a 403 forbidden response when calling sharepoint.example.com/sites/ExampleSite/_api/search/query?querytext='some thing'

In Azure, the app registration of the external app, I have given it "Sites.ReadWrite.All" - tried it both delegated and application level.

enter image description here

For the OAuth login, I have configured it to request the same permission:

      authorization: {
        params: {
          scope: 'openid email profile Sites.ReadWrite.All',
          prompt: 'consent',
        },
      },

And it does pop up the "Do you want to allow this app to do ... " dialog when logging in.

enter image description here

And after login succeeds, it returns an access_token that looks something like

ey...really really long string ... Q

And finally when I call the REST API, I pass that access token back:

    const result = await fetch(`${sharepointUrl}/_api/search/query?querytext='${searchtext}'`, {
      headers: {
        Bearer: `ey.......Q`,
        Accept: 'application/json;odata=verbose',
        'odata-version': '',
      },
    })

But the result is 403 Forbidden. Obviously I've missed something, but what?

Response {
      requestIncludesCredentials: true,
      type: 'default',
      status: 403,
      statusText: 'Forbidden',
    },
  }

I am an admin on the Sharepoint, and I am creating this app so I have full permissions inside Azure for both.

1 Answer 1

1
  1. You give the Graph permission in app, but you want to use the SharePoint Search api. And your administrator has not yet consented to the application’s permissions.
  2. If you need to grant access to SP Rest api via Azure AD App, you need to use a certificate to request access. Take a look at this :https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread

Updated: enter image description here

6
  • Thanks. #1 Do you mean Sites.Search.All? I have now granted admin permission, added Sites.Search.All and the response is still 403. I have decoded the access_token and can confirm that the SCP section does correctly contain those permissions.
    – JK.
    Commented Aug 8 at 2:29
  • #2 Do you mean via application permission instead of delegated? I am trying to use delegated permissions. But I might end up also trying application only as well.
    – JK.
    Commented Aug 8 at 2:31
  • Is there any setting inside of Sharepoint that also needs to be set? So far I have only set permissions inside Azure's app registration portal. I am an admin on SP, and can search the site via web browser, so delegated permissions should theoretically work.
    – JK.
    Commented Aug 8 at 2:32
  • Look at the part marked in my updated image, you should use the Graph api if you assign Graph permissions, and the rest api only if you assign SharePoint permissions!
    – Amos
    Commented Aug 8 at 13:20
  • Thanks - I will see if the Graph API is a better choice. I have chosen the Sharepoint permissions (admin granted too), and using the REST API. So they should go together but I am still seeing only a 403.
    – JK.
    Commented Aug 13 at 3:49

Your Answer

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

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