Search code examples
powershell-4.0

Invoke-WebRequest - Authorization has been denied for this request


I'm trying to access API using Powershell Invoke-WebRequest, response of it is always "Authorization has been denied for this request" . however i fetched Access_token and punched into Postman and it works fine ,I am getting 200 response . So trying to figure out where i am going wrong within powershell.

My code Below

$loginInfo = @{
            Username = $configData.UN
            Password = $configData.PS
        }

$loginInfo =  $loginInfo | convertTo-Json -Depth 12
$authResult = Invoke-RestMethod  $url -Method POST  -ContentType "application/json" -Body $loginInfo
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", 'Bearer '+ $authResult.access_token)

Invoke-WebRequest -Method Get -Uri $url -Headers $headers

Solution

  • I figured it out, for some reason URL is case sensitive, i changed one of the URI segment from Uppercase to Lowercase and it started working. Strange but yes issue is fixed.