ついにきましたね!
誰得 PowerShell on MacOS!
https://github.com/PowerShell/PowerShell
誰得感満載なんで、今日は OAuth.jp じゃなくて Qiita に記事あげてみましょうね!
さ、ではさっそく PowerShell 祝いってことで、Graph API にアクセスしてみましょう。
あ、FB の方ね。
#!/usr/local/bin/powershell -File
$access_token = Read-Host "token"
$headers = @{
Authorization = "Bearer $access_token";
}
$response = Invoke-RestMethod 'https://graph.facebook.com/me' -Headers $headers
$response.id
$response.name
$response | ConvertTo-Json
上記のようなスクリプトを graph_api_client_sample.ps1
ってファイル名で保存して、Terminal からそいつを実行すればよいようです。
user@tovan Desktop$ chmod +x graph_api_client_sample.ps1
user@tovan Desktop$ ./graph_api_client_sample.ps1
ps.
ついでに以下のように OAuth Dance するスクリプトも書いてみたんですけど、なんか 400 エラー返ってきてて、かつ実際の HTTP Request and/or Response を見る方法がわかんないんで挫折なぅです。
おしえて PowerShell のパパ!
#!/usr/local/bin/powershell -File
$client_id = Read-Host "Client ID"
$client_secret = Read-Host "Client Secret"
$redirect_uri = Read-Host "Redirect URI"
$authz_endpoint = "https://graph.facebook.com/oauth/authorize"
$token_endpoint = "https://graph.facebook.com/oauth/token"
open ($authz_endpoint + "?client_id=" + $client_id + "&redirect_uri=" + $redirect_uri)
$code = Read-Host "code"
$params = @{
client_id = $client_id;
client_secret = $client_secret;
code = $code;
grant_type = "authorization_code";
redirect_uri = $redirect_uri;
}
$result = Invoke-RestMethod -Uri $token_endpoint -Method Post -Body $params
$result.access_token
ちなみに返ってくるエラーはこんな感じ。
Invoke-RestMethod : Response status code does not indicate success: 400 (Bad Request).
At /Users/user/Desktop/oauth_client.ps1:22 char:11
+ $result = Invoke-RestMethod -Uri $token_endpoint -Method Post -Body $ ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: POST, R...m-urlencoded
}:HttpRequestMessage) [Invoke-RestMethod], HttpRequestException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand