Invoke-WebRequest Authentication with SharePoint Online

%3CLINGO-SUB%20id%3D%22lingo-sub-1528474%22%20slang%3D%22en-US%22%3EInvoke-WebRequest%20Authentication%20with%20SharePoint%20Online%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1528474%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20do%20you%20authenticate%20with%20SharePoint%20Online%20with%20Invoke-WebRequest%3F%20I%20have%20tried%20following%20which%20returns%20status%20200%2C%20OK%20but%20it%20doesn't%20mean%20login%20was%20successful.%20I%20would%20like%20to%20list%20all%20URLs%20from%20a%20site.%20the%20%24response.Links%20doesn't%20return%20anything.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3Etry%0A%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24response%26nbsp%3B%3D%26nbsp%3BInvoke-WebRequest%26nbsp%3B-Uri%26nbsp%3B%22https%3A%2F%2Fxxx365.sharepoint.com%22%26nbsp%3B-Credential%26nbsp%3B(Get-Credential)%26nbsp%3B-ErrorAction%26nbsp%3BStop%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%23%26nbsp%3BThis%26nbsp%3Bwill%26nbsp%3Bonly%26nbsp%3Bexecute%26nbsp%3Bif%26nbsp%3Bthe%26nbsp%3BInvoke-WebRequest%26nbsp%3Bis%26nbsp%3Bsuccessful.%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24StatusCode%26nbsp%3B%3D%26nbsp%3B%24Response.StatusCode%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24response%0A%7D%0Acatch%0A%7B%0A%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%24StatusCode%26nbsp%3B%3D%26nbsp%3B%24_.Exception.Response.StatusCode.value__%0A%7D%0A%24StatusCode%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1528474%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Highlighted
Occasional Contributor

How do you authenticate with SharePoint Online with Invoke-WebRequest? I have tried following which returns status 200, OK but it doesn't mean login was successful. I would like to list all URLs from a site. the $response.Links doesn't return anything.

 

 

try
{
    $response = Invoke-WebRequest -Uri "https://xxx365.sharepoint.com" -Credential (Get-Credential) -ErrorAction Stop
    # This will only execute if the Invoke-WebRequest is successful.
    $StatusCode = $Response.StatusCode
    $response
}
catch
{
    $StatusCode = $_.Exception.Response.StatusCode.value__
}
$StatusCode

 

 

0 Replies