我正在尝试将Twilio上的记录下载到服务器本地文件系统上的文件中(以便我可以将它们发送到另一个存储位置),但是按照我发现的代码,JsonConvert会抛出错误.DeserializeObject()调用。
我在这里找到的代码(称为“检索实际的记录媒体”):https://www.twilio.com/docs/video/api/recordings-resource#filter-by-participant-sid
代码如下:
static void Main(string[] args)
{
// Find your Account SID and Auth Token at twilio.com/console
const string apiKeySid = "SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const string apiKeySecret = "your_api_key_secret";
const string recordingSid = "RTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
const string uri = $"https://video.twilio.com/v1/Recordings/{recordingSid}/Media";
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(apiKeySid + ":" + apiKeySecret)));
request.AllowAutoRedirect = false;
string responseBody = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
var mediaLocation = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody)["redirect_to"];
Console.WriteLine(mediaLocation);
new WebClient().DownloadFile(mediaLocation, $"{recordingSid}.out");
}
这是我的版本:
var twilioRecordingUri = $"https://api.twilio.com/2010-04-01/Accounts/{recording.AccountSid}/Recordings/{recording.Sid}.mp3?Download=true";
var request = (HttpWebRequest)WebRequest.Create(new Uri(twilioRecordingUri));
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes($"{apiKeySid}:{apiKeySecret}")));
request.ContentType = "audio/mpeg";
//request.Accept = "audio/mpeg";
request.AllowAutoRedirect = false;
var responseBody = new StreamReader(request.GetResponse().GetResponseStream()).ReadToEnd();
var deserialized = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var mediaLocation = deserialized["redirect_to"];
new WebClient().DownloadFile(mediaLocation, $"{recording.Sid}.out");
但是执行该代码,它在JsonConvert.Deserialize()上失败,就像我提到的那样;它将引发此通用Json错误:
解析值�时遇到意外字符。路径“,行 0,位置0。
将鼠标悬停在我的“ responseBody”变量上确实表明这是一串很长的时髦字符。
我的想法是我应该在“ audio / mpeg”中添加“ Accept”或“ Content-type”,因为这是我要检索的文件类型。但是,当同时在请求和响应标头上检查开发工具时,Accept或Content-type都无法获得我刚刚指定的音频/ mpeg设置。
这里的代码有什么问题?
编辑:对于发现下载URL与Twilio示例不同的任何人,我发现此页面具有更新的URL:How do I get a call recording url in twilio when programming in PHP?
答案 0 :(得分:1)
您似乎要拨打的电话是下载.mp3
文件而不是Dictionary<string, string>
,因此在尝试将字符串反序列化为该类型时可能会遇到错误不匹配。结果可能会看到一个Base64
字符串,尤其是根据您的描述。我不确定是否至少没有看到数据样本,但是我猜您正在下载原始.mp3
文件,而不是带有位置(redirect_to)的文件信息。
如果结果是纯Base64
字符串,则应该能够将其转换为字节数组,并将其直接写入具有所需文件名的文件中。那应该为您提供所需的mp3
文件。
答案 1 :(得分:1)
我仅发布此答案以显示“工作”版本的外观。是@Grungondola的回答提示了我。感谢他(以及接受的答案)。
private async Task DownloadRecording(RecordingResource recording, string fileName)
{
if (string.IsNullOrEmpty(fileName))
throw new ArgumentNullException("fileName is required when downloading a recording.");
var twilioRecordingUri = $"https://api.twilio.com/2010-04-01/Accounts/{recording.AccountSid}/Recordings/{recording.Sid}.mp3?Download=false";
var request = (HttpWebRequest)WebRequest.Create(new Uri(twilioRecordingUri));
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes($"{apiKeySid}:{apiKeySecret}")));
request.ContentType = "audio/mpeg";
request.AllowAutoRedirect = false;
var stream = request.GetResponse().GetResponseStream();
var virtualPath = HttpContext.Server.MapPath(fileName);
var fileStream = new FileStream(virtualPath, FileMode.Create);
await stream.CopyToAsync(fileStream);
fileStream.Close();
}