使用可编程语音 SDK 在 Twilio 中检索所有录音

Retrieve All Recordings on Twilio Using Programable Voice SDK

我正在尝试下载与 Twilio 帐户关联的所有 Recording 对象,但在 TwilioVoiceClient.h 或 VoiceClient.h 中没有看到任何实现此目的的方法。我正在使用 ProgramableVoice iOS SDK(测试版 5),但在线文档没有显示任何 objective-c 或 Swift。如果 Recording SID 是已知的,我可以毫无问题地播放个人录音:

NSString *baseString = @"https://api.twilio.com/2010-04-01/Accounts/";
NSString *recordingSID = @"RExxxxxxxxxxxxxxxxxxxxxxxxxxxxx";    
NSURL *recordingUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/Recordings/%@.mp3", baseString, kTwilAccount, recordingSID]];
avPlayer = [[AVPlayer alloc]initWithURL:recordingUrl];

不过,我想做的是为一个帐户下载所有关联的记录对象。为此,我求助于 NSUrlConnection:

NSString *baseStr = [NSString stringWithFormat:@"https://api.twilio.com/2010-04-01/Accounts/%@/Recordings.json", kTwilAccount];
NSURL *url = [NSURL URLWithString:baseStr];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:1000.0];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", kTwilAccount, authToken];
NSData *authData = [authStr dataUsingEncoding:NSASCIIStringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Base %@", [authData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];

[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responseCode, NSData *responseData, NSError *responseError) {
    if ([responseData length] > 0 && responseError == nil){
        // do work
    } else if ([responseData length] == 0 && responseError == nil){
        NSLog(@"data error: %@", responseError);

    } else if (responseError != nil && responseError.code == NSURLErrorTimedOut){
        NSLog(@"data timeout: %ld", (long)NSURLErrorTimedOut);


    } else if (responseError != nil){
        NSLog(@"data download error: %@", responseError);


    }
}];

这会导致控制台输出下载错误:

数据下载错误:Error Domain=NSURLErrorDomain Code=-1012 "(null)" UserInfo={NSErrorFailingURLStringKey=https://api.twilio.com/2010-04-01/Accounts/ACdee27262ef5fd27a593a697d80e7f7b0/Recordings.json, NSUnderlyingError=0x17084aa70 {Error Domain=kCFErrorDomainCFNetwork Code=-1012 "(null)" UserInfo={_kCFURLErrorAuthFailedResponseKey={url = https://api.twilio.com/2010-04-01/Accounts/ACdeedkfdjieireijrekjrkejrk4kj4/Recordings.json}}}, NSErrorFailingURLKey=https://api.twilio.com/2010-04-01/Accounts/ACdeedkfdjieireijrekjrkejrk4kj4/Recordings.json}

显然,它要么无法识别端点,要么不喜欢我的身份验证方式。我应该如何在 objective-c 中请求此信息?

感谢阅读。

这里是 Twilio 开发人员布道者。

我们实际上不建议您将帐户凭据放入应用程序并从您的应用程序进行 API 调用。恶意攻击者可能会反编译您的应用程序、检索您的凭据并滥用您的帐户。

我们建议您实施服务器端解决方案来调用 API 并检索您的录音。然后您可以从应用程序与您的服务器通信。

Here's a blog post that explains more and includes an example for sending an SMS from an iOS application.