Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active February 6, 2016 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aetos382/e737286380e99cfaf9e8 to your computer and use it in GitHub Desktop.
Save aetos382/e737286380e99cfaf9e8 to your computer and use it in GitHub Desktop.
VoiceText API は 401 Unauthorized レスポンスで WWW-Authenticate ヘッダーを返さないので、.NET で使うときは Credentials を使わずに Authorization ヘッダーを自分で組み立ててやらないといけないらしいっす。
<#
.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