【发布时间】:2014-12-27 09:49:46
【问题描述】:
我想从 Powershell 调用 Google API。谷歌文档说:
curl "https://accounts.google.com/o/oauth2/token"
-d "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$CODE&grant_type=authorization_code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
从命令行执行时有效。
我尝试将其转换为 powershell,例如:
$token = Invoke-WebRequest -Uri "https://accounts.google.com/o/oauth2/token" -Method POST -Body "client_id=x&client_secret=y&code=z&grant_type=authorization_code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
比如:
$tokenParams = @{
client_id='x';
client_secret='y';
code='z';
grant_type='authorization_code';
redirect_uri='urn:ietf:wg:oauth:2.0:oob'
}
$token = Invoke-WebRequest -Uri "https://accounts.google.com/o/oauth2/token" -Method POST -Body $tokenParams
两者都返回:
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
如何正确地将 curl 转换为 Invoke-WebRequest 或其他 PS cmdlet?
【问题讨论】:
-
还可以查看
Invoke-RestMethod,我在 Powershell V3 及更高版本中可用。 -
但是,这里真正的问题是您将对象作为一个主体传递。 Google 的所有 API 都希望这些作为参数,它们位于请求标头中,而不是正文中。另外,你有没有看过这个免费的 powershell API 实现 github.com/squid808/gShell
标签: powershell curl