我正在Powershell中运行下面的代码。我得到一个错误返回“给定的客户凭据无效”。我正在尝试执行使用API的第一步,生成访问令牌。我已经确认我有一个有效的client_id和client_secret。
这是在邮递员工作,但我没有看到我的错误在Powershell。帮助!
$URLTokenV2="https://accounts.adp.com/auth/oauth/v2/token?grant_type=client_credentials"
$Json= @{
client_id='xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
client_secret='xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxx'
}
$Json=$Json | ConvertTo-Json
$Cert = Get-ChildItem -Path "Cert:\LocalMachine\My" | Where-Object {$_.Subject -like "CN=CertCN*"}
$Response=Invoke-RestMethod -Uri $URLTokenV2 -Method Post -Body $Json -Certificate $Cert -ContentType 'application/x-www-form-urlencoded'我也尝试过调用-RestMethod和Invoke-WebRequest。我得到了相同的结果,即客户端凭据无效。
我们对此表示感谢。提前谢谢。
发布于 2021-06-11 20:47:22
来自ADP的公开文件
通常,使用者应用程序应该使用HTTP身份验证方案(或其他指定方案)在header中传递client_id和client_secret参数。client_id和client_secret必须用一个冒号(":")分隔,并按照IETF 2617的要求在base64 64编码的字符串中编码。 您的消费者应用程序必须:
POST /auth/oauth/v2/token HTTP/1.1主机: accounts.adp.com授权:基本QURQVGFibGV0OnRoZXRhYmxldHBhc3N3b3Jk内容-类型:application/x form-urlencoded grant_type=client_credentials
所以你需要这样做:
$URLTokenV2 = 'https://accounts.adp.com/auth/oauth/v2/token'
# Prepare Basic Authentication header payload (the base64 part)
$AuthorizationValue = [Convert]::ToBase64String(
[System.Text.Encoding]::UTF8.GetBytes('xxxxxxxxx-client-id-xxxxxxxxx:xxxxxx-client-secret-xxxxxxxxxxxx')
)
$Cert = Get-ChildItem -Path "Cert:\LocalMachine\My" | Where-Object {$_.Subject -like "CN=CertCN*"}
# Prepare request body - no need to explicitly convert to JSON
$Body = @{
grant_type = 'client_credentials'
}
# Pass the `Authorization` header value with the payload we calculate from the id + secret
$Response = Invoke-RestMethod -Uri $URLTokenV2 -Method Post -Header @{ Authorization = "Basic ${AuthorizationValue}" } -Body $Body -Certificate $Cert -ContentType 'application/x-www-form-urlencoded'发布于 2021-06-11 20:57:39
在Mathias的评论之后,我现在有了以下工作
$URLTokenV2="https://accounts.adp.com/auth/oauth/v2/token?"
$Json= @{
client_id='xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx'
client_secret='xxxxxx-xxxxx-xxxx-xxxx-xxxxxxxxxxxx'
grant_type='client_credentials'
}
$Response=Invoke-RestMethod -Uri $URLTokenV2 -Method Post -Body $Json -Certificate $Cert -ContentType 'application/x-www-form-urlencoded'我尝试过将“grant_type=‘client_凭据’”作为Json数组的一部分,但这一次我尝试时意外地离开了'?‘在URL中。我还得把线路移开以转换-Json。在那之后它起作用了。
https://stackoverflow.com/questions/67942972
复制相似问题