【问题标题】:Unable to connect to Room: Invalid Access Token issuer/subject twilio无法连接到 Room:无效的访问令牌发行者/主题 twilio
【发布时间】:2021-03-03 17:10:35
【问题描述】:

我确实想在后端创建一个访问令牌,并且需要传递到前端才能连接到视频聊天室。

这是我的后端代码

const twilioAccountSid = process.env.twilioAccountSid;
const twilioApiKey = process.env.twilioApiKey;
const twilioApiSecret = process.env.twilioApiSecret;

const room = "cool room";

app.post("/access-token", (req, res) => {
  try {
    console.log(
      "sid",
      twilioAccountSid,
      "key",
      twilioApiKey,
      "secret",
      twilioApiSecret
    );
    const identity = "user";

    // Create Video Grant
    const videoGrant = new VideoGrant({
      room,
    });

    // Create an access token which we will sign and return to the client,
    // containing the grant we just created
    const token = new AccessToken(
      twilioAccountSid,
      twilioApiKey,
      twilioApiSecret,
      { identity: identity }
    );
    token.addGrant(videoGrant);

    // Serialize the token to a JWT string
    console.log(token.toJwt());
    res.status(200).json(token.toJwt());
  } catch (error) {
    console.warn(error);
    res.sendStatus(500);
  }
});

对于 Twilio 帐户 SID,我使用了仪表板的 SID,它从 AC 开始 对于 API 密钥,我添加了在创建 API 密钥时给它的友好名称。 API secret 是该 API 密钥的密钥 ID。

成功创建令牌并传递给前端。

这是我的前端代码

const connectRoom = async () => {
  try {
    const token = await axios.post("http://localhost:5000/access-token");

    connect(token.data, { name: roomName, video: { width: 640 } }).then(
      (room) => {
        console.log(`Successfully joined a Room: ${room}`);
        room.on("participantConnected", (participant) => {
          console.log(`A remote Participant connected: ${participant}`);
          participant.tracks.forEach((publication) => {
            console.log("for each");
            if (publication.isSubscribed) {
              const track = publication.track;
              document
                .getElementById("remote-media-div")
                .appendChild(track.attach());
            }
          });
          participant.on("trackSubscribed", (track) => {
            document
              .getElementById("remote-media-div")
              .appendChild(track.attach());
          });
        });
      },
      (error) => {
        console.error(`Unable to connect to Room: ${error.message}`);
      }
    );
  } catch (error) {
    console.log(error);
  }

然后我得到这个错误

无法连接到 Room:访问令牌颁发者/主题无效

我该如何解决这个问题?

任何帮助! 提前致谢

【问题讨论】:

    标签: javascript twilio twilio-api


    【解决方案1】:

    您可以在此处创建 API 密钥(或通过 Console)。注意,API Key 以SK...开头。

    REST API: API Keys

    【讨论】:

    • 非常感谢。你拯救了一天。 =) APIKEY 应该以 SK 开头
    猜你喜欢
    • 2021-01-30
    • 2020-01-26
    • 2023-03-09
    • 2017-06-24
    • 2020-09-09
    • 2020-05-06
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多