使用Powershell调用Google API [英] Call Google API with Powershell

查看:366
本文介绍了使用Powershell调用Google API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Powershell调用Google API。 Google文档说:

  curlhttps://accounts.google.com/o/oauth2/token
-dclient_id = $ CLIENT_ID& client_secret = $ CLIENT_SECRET& code = $ CODE& grant_type = authorization_code& redirect_uri = urn:ietf:wg:oauth:2.0:oob



我试图将其转换为powershell类似:

p>

  $ token = Invoke-WebRequest -Urihttps://accounts.google.com/o/oauth2/token-Method POST -Bodyclient_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 -Urihttps://accounts.google.com / o / oauth2 / token-Method POST -Body $ tokenParams



Invoke-WebRequest:远程服务器返回错误:(400)错误请求。



如何正确地将curl转换为Invoke-WebRequest或另一个PS cmdlet?

解决方案

> Invoke-WebRequest ,因为POST请求的主体应具有在PS中默认设置的 application / x-www-form-urlencoded 内容类型。



我执行了相同的代码,我得到了一个401请求,因为我的auth参数不匹配。我也在Fiddler中检查过它,并且消息正确构造了



Mine Fiddler捕获的Invoke-WebRequest POST消息如下:

  POST https://accounts.google.com/o/oauth2/token HTTP / 1.1 
User-Agent:Mozilla / 5.0(Windows NT; Windows NT 6.4 ; hu-HU)WindowsPowerShell / 5.0.9879.0
Content-Type:application / x-www-form-urlencoded
Host:accounts.google.com
Content-Length:113
预期:100-继续
连接:Keep-Alive

grant_type = authorization_code& redirect_uri = urn%3aietf%3awg%3aoauth%3a2.0%3aoob& code = z& client_id = x& ; client_secret = y

根据Google的oauth示例消息( https://developers.google.com/accounts/docs/OAuth2ForDevices )这是正确的,所以我会在Fiddler检查你的电话。


I want to call Google APIs from Powershell. The google documentation says:

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"

Which works when executed from the commandline.

I tried to convert this to powershell like:

$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"

And like:

$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

Both return:

Invoke-WebRequest : The remote server returned an error: (400) Bad Request.

How to properly convert curl to Invoke-WebRequest or another PS cmdlet?

解决方案

Theoretically you use Invoke-WebRequest right as the POST request's body should have application/x-www-form-urlencoded content type which is set by default in PS.

I executed the same code and I got a 401 request, because my auth parameters doesn't match. I also checked it in Fiddler too and the message is constructed correctly

Mine Fiddler captured Invoke-WebRequest POST message looks like:

POST https://accounts.google.com/o/oauth2/token HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.4; hu-HU) WindowsPowerShell/5.0.9879.0
Content-Type: application/x-www-form-urlencoded
Host: accounts.google.com
Content-Length: 113
Expect: 100-continue
Connection: Keep-Alive

grant_type=authorization_code&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&code=z&client_id=x&client_secret=y

According to Google's oauth sample message ( https://developers.google.com/accounts/docs/OAuth2ForDevices ) this is correct, so I would check your call in Fiddler too.

这篇关于使用Powershell调用Google API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆