Anyone have experience using PowerShell with Oauth2 authorization. I have a Dell TechDirect key API ID and API Secret. I can object the initial token without a problem but when I try to acquire the authorization, my script fails.
I’ve obfuscated the API ID and key, but I did a direct copy and paste from Dell’s website.
#My Dell Service Tag $tag = "J088K72" # Get Access Token - expires in 3600 seconds $accessTokenEndpointUrl = "https://apigtwb2c.us.dell.com/auth/oauth/v2/token" $requestBody = @{ grant_type = "client_credentials" client_id = "XXXXXXXXXXX" # api id client_secret = "XXXXXXXX" # api secret } $token = Invoke-RestMethod -Method Post -Uri $accessTokenEndpointUrl -Body $requestBody -ContentType "application/x-www-form-urlencoded" # url for api warranty query - tag is comma separated list of service tags $url = "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements" $headers = @{"Authorization" = "Bearer $token" } $body = @{ servicetags = $tag } Invoke-RestMethod -URI $url -Method GET -contenttype 'Application/xml' -Headers $headers -body $body
This is the output
Invoke-RestMethod : Invalid Authentication At C:\Users\bclanton\Documents\REPOS\TP_PSCode\TechDirect\Get-DellAsset.ps1:21 char:2 + Invoke-RestMethod -URI $url -Method GET -contenttype 'Application/xm ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
This is to show that I do have a valid token
access_token token_type expires_in scope ------------ ---------- ---------- ----- d0fb4c9f-7732-4b76-b3e0-13fb0640b68d Bearer 3600 oob