Sharepoint API Authentication - Powershell

M Usman Siddiqui 0 Reputation points
2023-05-23T06:01:32.5466667+00:00

Hello Everyone,

I want to use Sharepoint's Rest API to read and write to different documents but I dont know to get the auth token. Can any one explain the process to generate/create an auth token and how to use in a script to read an excel sheet?

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,411 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 30,186 Reputation points Microsoft Vendor
    2023-05-23T07:59:49.8333333+00:00

    Hi @M Usman Siddiqui,

    If you want to use Sharepoint's Rest API to read a file in sharepoint. You will need register a SharePoint app only first. Please refer to the following steps to register a sharepoint app first in the document

    https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs

    Then you can use the client id and secret to connect sharepoint with pnp powershell and use Invoke-PnPSPRestMethod to call rest api

    #Site collection URL
    $SiteURL = "https://crescent.sharepoint.com/sites/xxx"
    #Connect to SharePoint Online with ClientId and ClientSecret
    Connect-PnPOnline -Url $SiteURL -ClientId "yourclientid" -ClientSecret "yoursecret"
    $output = Invoke-PnPSPRestMethod -Url '/_api/web/lists?$select=Id,Title'
    $output.value
    

    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.