从 Alexa 语音服务获取 500 错误
Posted
技术标签:
【中文标题】从 Alexa 语音服务获取 500 错误【英文标题】:Getting a 500 error from alexa voice services 【发布时间】:2015-12-14 22:59:37 【问题描述】:我有一个正在录制语音命令的 AVAudioRecorder,然后我使用以下代码将其转换为 nsdata -
NSData *data = [NSData dataWithContentsOfURL:recorder.url];
现在我使用以下代码执行语音识别器请求 -
NSDictionary *headers = @ @"Authorization": [NSString stringWithFormat:@"Bearer %@", authCode], @"Transfer-Encoding": @"chunked", @"Content-Type": @"multipart/form-data;boundary=someboundary" ;
NSMutableData *postData = [[NSMutableData alloc] initWithData:[@"\r\n--someboundary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"request\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: application/json; charset=UTF-8\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"messageHeader\": \r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"deviceContext\": [\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"name\": \"playbackState\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"namespace\": \"AudioPlayer\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"payload\": \r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"streamId\": \"\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // what is the streamid supposed to be?
[postData appendData:[@"\"offsetInMilliseconds\": \"\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];//Is this correct?
[postData appendData:[@"\"playerActivity\": \"IDLE\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"]\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"messageBody\": \r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"profile\": \"alexa-close-talk\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"locale\": \"en-us\",\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\"format\": \"audio/L16; rate=16000; channels=1\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"--someboundary\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Disposition: form-data; name=\"audio\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:[NSData dataWithData:audioData]];
[postData appendData:[[NSString stringWithFormat:@"\r\n--someboundry--"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
if (error)
NSLog(@"%@", error);
else
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSLog(@"SpeechRecognition %@", httpResponse);
];
[数据任务恢复];
但是上面的代码给了我一个 500 错误。非常感谢任何帮助
【问题讨论】:
【参考方案1】:我知道出了什么问题。音频数据之前的内容类型应该是
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
不是
[postData appendData:[@"Content-Type: audio/L16; rate=16000; channels=1\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
它缺少一个额外的“\r\n”
【讨论】:
以上是关于从 Alexa 语音服务获取 500 错误的主要内容,如果未能解决你的问题,请参考以下文章
使用烧瓶询问的 Alexa 技能:HTTP 500:内部服务器错误
从 HttpClient 获取 500 错误,在浏览器中工作