无法检索/下载实际录制媒体Twilio

时间:2017-10-11 14:31:56

标签: node.js twilio twilio-api

const Twilio = require('twilio');
const request = require('request');

const apiKeySid = 'SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const apiKeySecret = 'your_api_key_secret';
const accountSid = 'ACb46a83261c60f3a45ce47eccac8a913d';
const client = new Twilio(apiKeySid, apiKeySecret, { accountSid: 
accountSid });

const roomSid = 'RMXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const recordingSid = 'RTXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const uri = 'https://video.twilio.com/v1/' +
        `Rooms/${roomSid}/` +
        `Recordings/${recordingSid}` +
        '/Media';
const response = client.request({ method: "POST", uri: uri });
const mediaLocation = JSON.parse(response.body).location;

request.get(mediaLocation, (err, res, media) => {
console.log(media);
});

我尝试检索录制的媒体,但是当我打印response.body时,这不起作用并且未定义。

2 个答案:

答案 0 :(得分:1)

Twilio开发者传道者在这里。

道歉,这是我们文档中的错误。调用client.request是异步调用并返回Promise

请改为尝试:

client.request({ method: "GET", uri: uri }).then(response => {
  const mediaLocation = JSON.parse(response.body).location;
  request.get(mediaLocation, (err, res, media) => {
    console.log(media);
  });
});

我已经开始a pull request to get this back into the documentation here too

答案 1 :(得分:0)

@philnash我在上面的代码块中尝试了这个但是得到了" mediaLocation"未定义。

JSON.parse(response.body).location 更改为 JSON.parse(response.body).redirect_to 并且&# 39;工作。

 client.request({ method: "GET", uri: uri }).then((response) => {
 const mediaLocation = JSON.parse(response.body).redirect_to;
 request.get(mediaLocation, (err, res, media) => {
  console.log(media);
 });
});

响应:

{ statusCode: 302,
body: '{"redirect_to": 
"https://xxxxxxxxxxxxx.s3.amazonaws.com/xxxxxxxxxxxxxxxxxxxxx…….”}’ }