我正在尝试使用我的 iOS 应用程序中的 Google Cloud Speech Api 转录长文件(> 1 分钟)。
为此,我执行以下操作:
1) 如文档中所写,我匿名向 firebase 验证用户身份
[[FIRAuth auth]
signInAnonymouslyWithCompletion:^(FIRUser *_Nullable user, NSError *_Nullable error) {
// ...
if (!error){
self.user = user;
[self.user getTokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
if (!error) {
self.token = token;
[self uploadFile];
}
}];
}
}];
2)然后我将文件上传到 Firebase 存储并获取 URI
- (void)uploadFile
{
FIRStorage * storage = [FIRStorage storage];
FIRStorageReference *storageRef = [storage reference];
FIRStorageReference *sonetRef = [storageRef child:@"transcribeTest/sonet130.mp3"];
NSData *sonet = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sonet130" ofType:@"mp3"]];
FIRStorageUploadTask *uploadTask = [sonetRef putData:sonet metadata:nil completion:^(FIRStorageMetadata * _Nullable metadata, NSError * _Nullable error) {
if (error != nil) {
// Uh-oh, an error occurred!
} else {
// Metadata contains file metadata such as size, content-type, and download URL.
NSURL *downloadURL = metadata.downloadURL;
NSString *path = [NSString stringWithFormat:@"gs://%@/%@",metadata.bucket,metadata.path];
NSURL * gsURL = [NSURL URLWithString:path];//@"gs://%@",downloadURL.path]];
[self trnscribeFileAtURL:gsURL];
}
}];
[uploadTask resume];
}
3) 下一步应该是向 Google Cloud Speech API 发送 Asyncrecognize 请求。但我得到 401 错误''。
-(void)trnscribeFileAtURL:(NSURL*)url
{
NSString *service = @"https:/speech.googleapis.com/v1beta1/speech:asyncrecognize";
service = [service stringByAppendingString:@"?key="];
service = [service stringByAppendingString:API_KEY];
NSData *audioData = [NSData dataWithContentsOfFile:[self soundFilePath]];
NSDictionary *configRequest = @{@"encoding":@"LINEAR16",
@"sampleRate":@(SAMPLE_RATE),
@"languageCode":@"en-UK",
@"maxAlternatives":@30};
NSDictionary *audioRequest = @{@"uri":url.absoluteString};
NSDictionary *requestDictionary = @{@"config":configRequest,
@"audio":audioRequest};
NSError *error;
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestDictionary
options:0
error:&error];
NSString *path = service;
NSURL *URL = [NSURL URLWithString:path];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
// if your API key has a bundle ID restriction, specify the bundle ID like this:
[request addValue:[[NSBundle mainBundle] bundleIdentifier] forHTTPHeaderField:@"X-Ios-Bundle-Identifier"];
NSString *contentType = @"application/json";
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
[request addValue:[NSString stringWithFormat:@"Bearer %@",self.token] forHTTPHeaderField:@"Authorization"];
[request setHTTPBody:requestData];
[request setHTTPMethod:@"POST"];
NSURLSessionTask *task =
[[NSURLSession sharedSession]
dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(),
^{
NSString *stringResult = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
_textView.text = stringResult;
NSLog(@"RESULT: %@", stringResult);
});
}];
[task resume];
}
响应 JSON:
2017-02-28 19:58:05.837337 Speech[8057:2054726] RESULT: {
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
看起来问题出在这行代码上:
[request addValue:[NSString stringWithFormat:@"Bearer %@",self.token] forHTTPHeaderField:@"Authorization"];
我正在尝试传递从 Firebase Anonymous auth 获得的令牌,但它不适合。我已经搜索了我无法找到如何为匿名用户获取正确凭据的获取。请帮忙。