Twilio client.notify returns 403

Twilio client.notify returns 403

当用户发送消息时,我需要在我的网络应用程序 (nextjs) 中发送通知。我关注了 this 但它不起作用。

我有以下代码:

export const notifyUser = async (_identity: string, _message: string) => {
  return await client.notify
    .services(config.TWILIO_CHAT_SERVICE_SID)
    .notifications.create({ body: _message, identity: [_identity] });
};

我调用 api notify-user 并调用 notifyUSer(identity, message)

Identity是user_id我用来生成token的方法如下:

export const tokenGenerator = (identity: string) => {
  // Create an access token which we will sign and return to the client
  const token = new AccessToken(
    config.TWILIO_ACCOUNT_SID,
    config.TWILIO_API_KEY,
    config.TWILIO_API_SECRET
  );

  // Assign the provided identity or generate a new one
  token.identity = identity || 'unknown';

  if (config.TWILIO_CHAT_SERVICE_SID) {
    // Create a "grant" which enables a client to use IPM as a given user,
    // on a given device
    const chatGrant = new ChatGrant({
      serviceSid: config.TWILIO_CHAT_SERVICE_SID,
      pushCredentialSid: config.TWILIO_FCM_CREDENTIAL_SID,
    });
    token.addGrant(chatGrant);
  }

  if (config.TWILIO_SYNC_SERVICE_SID) {
    // Point to a particular Sync service, or use the account default to
    // interact directly with Functions.
    const syncGrant = new SyncGrant({
      serviceSid: config.TWILIO_SYNC_SERVICE_SID || 'default',
    });
    token.addGrant(syncGrant);
  }

  // Serialize the token to a JWT string and include it in a JSON response
  return {
    identity: token.identity,
    token: token.toJwt(),
  };
};

我希望得到示例中的响应,但我收到以下错误: 你觉得我做错了什么?

与您的通知验证有关。 403 错误类似于 401 未授权错误,但区别在于 403 错误不允许您 re-authenticate 并禁止请求。检查您的身份验证部分。

您还可以查看 Twilio 调试指南:https://www.twilio.com/docs/usage/troubleshooting/debugging-your-application

Twilio Notify 是与 Twilio Chat 不同的服务,因此您尝试使用不正确的凭据来调用 API。您传递的是 Chat service sid, not a Notify service sid, and using the chat identity and not the identity of a Notify binding.

如果您尝试根据聊天消息发送通知,则应遵循此 guide on push notification configuration for chat

如果您正在尝试 send notifications outside of chat using Twilio Notify, follow this guide