none
Invalid Grant Type Powershell Invoke-WebRequest RRS feed

  • Question

  • I am writing a powershell script to request a access token. When I run it I am getting the error below for Invalid Grant type. I am not sure why (I am fairly new to powershell programming).

    Error:

    Invoke-WebRequest : Invalid grant type
    At C:\pwscripts\testps.ps1:34 char:1
    + Invoke-WebRequest -uri 'https://urlsite' -M ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
       eption
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

    The code I am using:

    $BodyArray = @(
        @{
            grant_type= 'refresh_token'
            refreshToken= 'refres token here'
     }
    )

    $BodyArray_Json = ConvertTo-Json -InputObject $BodyArray

    (New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

    Invoke-WebRequest -uri 'https://urlsite' -Method POST -body $BodyArray_Json -Headers @{'Accept' = 'application/json';'Content-Type' = 'application/json';}

    Wednesday, November 27, 2019 1:45 PM

All replies

  • There are many reasons for that error. You might want to try using something like Fiddler to see exactly what you're sending and what the response contains. It might be something a easy as using UTC instead of local time in the POST. It may be that you need to provide the authentication token together with the refresh token. But it may be something else entirely.

    I doubt it's a scripting problem, though. You'd probably get a better answer in some other forum that deals with IIS, web sites, HTML, etc.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Wednesday, November 27, 2019 4:06 PM
  • Correct formatting and structure for your code would help you o see the errors.

    $body = @{
        grant_type= 'refresh_token'
        refreshToken= 'refres token here'
    } | ConvertTo-Json 
    $headers = @{
        'Accept' = 'application/json'
        'Content-Type' = 'application/json'
    }
    
    $wc = New-Object System.Net.WebClient
    $wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    Invoke-WebRequest -uri 'https://urlsite' -Method POST -body $body -Headers $headers
    
    


    \_(ツ)_/

    Wednesday, November 27, 2019 5:43 PM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, December 6, 2019 7:13 AM
    Moderator