Last active
February 6, 2016 14:15
-
-
Save aetos382/e737286380e99cfaf9e8 to your computer and use it in GitHub Desktop.
VoiceText API は 401 Unauthorized レスポンスで WWW-Authenticate ヘッダーを返さないので、.NET で使うときは Credentials を使わずに Authorization ヘッダーを自分で組み立ててやらないといけないらしいっす。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <# | |
| .NET の HttpWebRequest は 401 Unauthorized レスポンスの WWW-Authenticate ヘッダーを見て、使用すべき認証方式(Basic、Digest、etc)を判断します。 | |
| VoiceText API は WWW-Authenticate ヘッダーを返さないので、HttpWebRequest は使用すべき認証方式を判断できず、エラーになってしまいます。 | |
| そのため、Authorization ヘッダーを自分で組み立てて Headers に指定してやらなければなりません。 | |
| Github API なんかも同様… | |
| #> | |
| $key = 'xxxxx' | |
| $token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${key}:")) | |
| $body = @{ | |
| text = 'VoiceText API は 401 Unauthorized レスポンスで WWW-Authenticate ヘッダーを返さないので、.NET で使うときは Credentials を使わずに Authorization ヘッダーを自分で組み立ててやらないといけないらしいっす。' | |
| speaker = 'show' | |
| } | |
| $headers= @{ | |
| Authorization = "Basic $token" | |
| } | |
| $response = Invoke-WebRequest -Uri 'https://api.voicetext.jp/v1/tts' -Method Post -Body $body -Headers $headers | |
| $player = New-Object 'System.Media.SoundPlayer' $response.RawContentStream | |
| $player.PlaySync() | |
| $player.Dispose() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment