2014-12-26 2 views
-1

Powershell에서 Google API를 호출하고 싶습니다. Google 설명서에서 다음과 같이 말합니다.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.

어떻게 제대로 Invoke- 할 컬 변환 WebRequest 또는 다른 PS cmdlet?

+0

Powershell V3 이상에서 사용할 수있는'Invoke-RestMethod'도 살펴보십시오. – Eris

+1

그러나 실제 문제는 개체를 몸체로 전달한다는 것입니다. Google의 모든 API는 이것을 매개 변수로 사용합니다. 매개 변수는 본문이 아닌 요청 헤더에 있습니다. 또한 powershell API 무료 구현 (https://github.com/squid808/gShell/)을 살펴 보았습니다. – Eris

답변

0

은 이론적으로 당신은 POST 요청의 몸이 응용 프로그램/PS에서 기본적으로 설정되어 콘텐츠 형식을 X-WWW-형태-urlencoded를해야 권리로 호출-WebRequest 클래스를 사용합니다.

동일한 인증 코드를 실행했고 인증 매개 변수가 일치하지 않아 401 요청을 받았습니다. 나는 또한 너무 피들러에서 그것을 확인하고 메시지가 제대로

을 구성되어

내 피들러처럼 호출 - WebRequest 클래스 POST 메시지가 보이는 캡처 : (https://developers.google.com/accounts/docs/OAuth2ForDevices)이이 올바른 구글의 OAuth를 샘플 메시지에 따르면

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 

, 그래서 Fiddler에서 전화를 확인합니다.