1

我只是想从客户端 ID、客户端密码和租户 ID 中获取访问令牌。以下 powershell 命令成功运行

Invoke-RestMethod -Uri https://login.microsoftonline.com/TENANT/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "CLIENTID"; "client_secret" = "SECRET" }

但是这个卷曲不起作用

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "grant_type=client_credentials&resource=https://management.core.windows.net/&client_id=CLIENTID&client_secret=SECRET" "https://login.microsoftonline.com/TENANT/oauth2/token?api-version=1.0"

这个红宝石脚本都不是

require 'json'
require 'typhoeus'

url = 'https://login.microsoftonline.com/TENANT/oauth2/token?api-version=1.0'
params = "grant_type=client_credentials&resource=https://management.core.windows.net/&client_id=CLIENTID&client_secret=SECRET"
HEADERS = {
"Content-Type" => "application/x-www-form-urlencoded"
}
resp = Typhoeus::Request.post(url, body: params, headers: HEADERS)

我正在关注这个链接。任何线索为什么 curl / ruby​​ 都不起作用?提前致谢

4

1 回答 1

0

我尝试成功重现您的问题,我发现问题是由没有 OpenSSL 的 curl 和没有设置的 Typhoeus 请求引起的ssl_verifypeer: false

因此,请按照在您的环境中检查curl --version并安装 openssl 库。

这是我的示例代码。

require "typhoeus"

url = 'https://login.microsoftonline.com/<tanet-id>/oauth2/token?api-version=1.0'
params = "grant_type=client_credentials&resource=https://management.core.windows.net/&client_id=<client-id>&client_secret=<client-key>"
headers = {
    "Content-Type" => "application/x-www-form-urlencoded"
}
request = Typhoeus.post(url, body: params, headers: headers, ssl_verifypeer: false)
puts request.code, request.body

希望能帮助到你。

于 2017-04-17T09:03:07.383 回答