Hi, I am using the Tutorial OAuth With Client Credentials with the powershell format.
We are in the US region.
The script I am using is a direct copy from the Tutorial:
$secret = "SECRET";
$id = "CLIENT"; auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("{id}:${secret}")) response = Invoke-WebRequest -Uri https://login.mypurecloud.com/oauth/token -Method POST -Body @{grant_type='client_credentials'} -Headers @{"Authorization"="Basic {auth}"}
$responseData = $response.content | ConvertFrom-Json
$roleResponse = Invoke-WebRequest -Uri https://api.mypurecloud.com/api/v2/authorization/roles -Method GET -Headers @{"Authorization"= $responseData.token_type + " " + $responseData.access_token}
write-host $roleResponse.content
The response I am getting is:
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send.
At C:\Users\steph\cr-x\getoauth.ps1:5 char:13
It's hard to say exactly what's going on in your code since there are partial stack traces from multiple errors and they don't match the code you shared and there's no details about what the actual response was (http status code and body). But the error about being unable to establish a TLS connection is most suspicious. Be sure your script is using TLS 1.1 or above when establishing a connection. TLS 1.0 is unsupported.
Thanks for the response. However the information I have provided for the Powershell attempt is from a single attempt but of course some of the errors refer to the latter code which is attempting to use information that was not retrieved from the initial login.
So changing tack then - with the curl script:
export TOKEN=cat oauth.txt
export CLIENT_ID=cat oauth.txt | awk -F":" '{print $1 }'
export CLIENT_SECRET=cat oauth.txt | awk -F":" '{print $2 }'
export TOKEN64=cat oauth.txt | 'base64'
With verbose mode on, the output is:
bash-3.2$ ./trial1.curl
sQUPOYg
token = YWI1YzY5ZGUtNDkzNi00MjE0LTkxYzctNmM1MmMzMjViNzE0Onp1L......LWFBNnNRVVBPWWcNCg0K
client_Id = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
client_secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying 54.152.195.5...
Please regenerate your client secret immediately. Your client secret, the base64 encoded id/secret, and the resulting auth token must always be handled like passwords. Do not post them publicly intact.
The "400 bad request" response means something is incorrect with your request body or headers. A spot check of the values looks like it's correct, so it's likely a syntax error somewhere in your script or incorrect cURL configuration that's causing the request to be malformed.