首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell ADP API令牌

Powershell ADP API令牌
EN

Stack Overflow用户
提问于 2021-06-11 20:07:13
回答 2查看 384关注 0票数 0

我正在Powershell中运行下面的代码。我得到一个错误返回“给定的客户凭据无效”。我正在尝试执行使用API的第一步,生成访问令牌。我已经确认我有一个有效的client_id和client_secret。

这是在邮递员工作,但我没有看到我的错误在Powershell。帮助!

代码语言:javascript
复制
$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。我得到了相同的结果,即客户端凭据无效。

我们对此表示感谢。提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-11 20:47:22

来自ADP的公开文件

通常,使用者应用程序应该使用HTTP身份验证方案(或其他指定方案)在header中传递client_id和client_secret参数。client_id和client_secret必须用一个冒号(":")分隔,并按照IETF 2617的要求在base64 64编码的字符串中编码。 您的消费者应用程序必须:

  • 使用注册期间提供的X.509证书发送请求。
  • 以URL编码格式传递所有参数,按照HTTP标头内容-Type:application/x form-urlencoded指定的UTF-8字符编码。实际请求可能类似于以下示例:

POST /auth/oauth/v2/token HTTP/1.1主机: accounts.adp.com授权:基本QURQVGFibGV0OnRoZXRhYmxldHBhc3N3b3Jk内容-类型:application/x form-urlencoded grant_type=client_credentials

所以你需要这样做:

代码语言:javascript
复制
$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'
票数 0
EN

Stack Overflow用户

发布于 2021-06-11 20:57:39

在Mathias的评论之后,我现在有了以下工作

代码语言:javascript
复制
$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。在那之后它起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67942972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档