How get an access token for sharepoint rest api?

Aj 0 Reputation points
2024-06-14T22:57:17.1366667+00:00

Hi, I would like to know the steps to generate an access token for a sharepoint rest api using application credentials.

I am trying to use the access token for provisioning lists and other assets in a sharepoint site via a sharepoint webpart app

If anyone can knows or can help, it would be awesome!

Regards,

--Aj

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,249 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,800 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2024-06-17T01:48:19.5566667+00:00

    Hi @Aj,

    To call SharePoint specific APIs you need to get a SPO specific access token. You can "swap" an regular MS Graph refresh token for an SPO specific token by doing the following:

    1. Get a delegated auth token from graph as you normally would (https://learn.microsoft.com/en-us/graph/auth-v2-user)
    2. Use the refresh_token you got and exchange it for an SPO access token by calling the auth endpoint again:
         POST https://login.microsoftonline.com/{{tenantName}}/oauth2/v2.0/token
      
    3. With the following form data:
         client_id=<APP ID>
         client_secret=<APP SECRET>
         refresh_token=<REFRESH TOKEN FROM ABOVE>
         grant_type=refresh_token
         scope=https://<YOUR TENANT NAME>.sharepoint.com/Sites.Read.All
      
    4. Take the access token and call the SPO API

    You must ensure your app is registered with the correct permissions. In the case above the app must have Sites.Read.All for example.

    Here is a nice article for reference

    https://sharepoint.stackexchange.com/questions/236286/sharepoint-online-rest-api-authentication-in-postman


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.