使用可编程语音SDK检索Twilio上的所有录制

时间:2017-01-20 21:45:57

标签: objective-c twilio nsurlconnection

我正在尝试下载与Twilio帐户关联的所有录制对象,但没有在TwilioVoiceClient.h或VoiceClient.h中看到实现此目的的任何方法。我正在使用ProgramableVoice iOS SDK(beta 5),但在线文档没有显示任何objective-c或Swift。如果记录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);


    }
}];

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

数据下载错误:错误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中请求此信息?

感谢阅读。

1 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

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

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

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