Using cURL in PowerShell

How cURL on PowerShell with an Autodesk Platform Services authentication endpoint example. Put this in a script (<script_name>.ps1) or copy and paste directly to the prompt.

$CLIENT_ID = '<Get from your App portal'
$CLIENT_SECRET = '<Get from your App portal>'
$CLIENT_CONVERT = $CLIENT_ID + ':' + $CLIENT_SECRET
$CLIENT_CONVERT
$creds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($CLIENT_CONVERT))
curl.exe -v 'https://developer.api.autodesk.com/authentication/v2/token' `
    -X 'POST' `
    -H 'Content-Type: application/x-www-form-urlencoded' `
    -H 'Accept: application/json' `
    -H "Authorization: Basic $creds" `
    -d 'grant_type=client_credentials' `
    -d 'scope=data:read'

Note that curl is a PowerShell alias for the Invoke-WebRequest cmdlet. You need to invoke curl.exe for it to behave like the bash version.

There are two approaches to use with Invoke-webRequest.

The first uses command line flags.

Invoke-WebRequest https://developer.api.autodesk.com/authentication/v2/token `
    -Headers @{Accept = 'application/json'; `
        Authorization = "Basic $creds" 
} `
    -ContentType 'application/x-www-form-urlencoded' `
    -Method 'Post' `
    -Body @{grant_type = 'client_credentials'; `
        scope          = 'data:read'
}

The second encapsulates all the parameters into a dictionary.

$LoginParameters = @{
    Uri     = 'https://developer.api.autodesk.com/authentication/v2/token'
    Method  = 'POST'
    Headers = @{
        Accept        = 'application/json'
        Authorization = "Basic $creds"
    }
    Content = 'application/x-www-form-urlencoded'
    Body    = @{grant_type = 'client_credentials'
        scope           = 'data:read'
    }
}

Invoke-WebRequest @LoginParameters

Reference:

Brian Johnson @brian3johnson