首页 > 解决方案 > Twilio 聊天用户 (Node.js) 友好名称在访问令牌创建期间未注册

问题描述

我正在尝试将Twilio 可编程聊天 SDK与我的 Firebase 函数集成,该函数是用 Typescript 编写的。

我想知道是否可以在创建访问令牌期间设置friendlyName 和属性值?

我尝试了以下方法,但仍然无济于事。

        const chatGrant = new ChatGrant({
            serviceSid: serviceSid,
            friendlyName : displayName,
            attributes : {
                profilePic : profilePic,
            }
        });
        const chatGrant = new ChatGrant({
            serviceSid: serviceSid,
        }, {
            friendlyName : displayName,
            attributes : {
                profilePic : profilePic,
            }
        });
        const chatGrant = new ChatGrant({
            serviceSid: serviceSid,
        });

        const token = new AccessToken(twilioAccountSid, twilioApiKey, twilioApiSecret);
        token.addGrant(chatGrant);
        token.identity = context.auth.uid;
        token.friendlyName : displayName,
        token.attributes : {
             profilePic : profilePic,
        }

这是整个代码:

        // displayName and profilePic initialization ...

        const AccessToken = require('twilio').jwt.AccessToken;
        const ChatGrant = AccessToken.ChatGrant;

        // Used when generating any kind of tokens
        const twilioAccountSid = functions.config().twilio.account_sid;
        const twilioApiKey = functions.config().twilio.api_key;
        const twilioApiSecret = functions.config().twilio.api_secret;

        // Used specifically for creating Chat tokens
        const serviceSid = functions.config().twilio.chat_service_sid;

        // Create a "grant" which enables a client to use Chat as a given user,
        // on a given device
        const chatGrant = new ChatGrant({
            serviceSid: serviceSid,
            friendlyName : displayName,
            attributes : {
                profilePic : profilePic,
            }
        });

        // 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);
        token.addGrant(chatGrant);
        token.identity = context.auth.uid;

        // Serialize the token to a JWT string
        return {identity: token.identity, token: token.toJwt()};

我在文档中找不到与此相关的任何内容。如果文档可以更清晰,那就太好了。特别是对于可编程聊天 SDK 部分。

例如,如果我们知道构造函数的参数(例如 ChatGrant 构造函数的参数是什么?),这对我们开发人员来说真的很有帮助。

谢谢一堆!

标签: firebasejwttwiliotwilio-programmable-chat

解决方案


在我看来,对于程序聊天,访问令牌仅用于访问聊天服务,因此没有与之相关的友好名称。成功进入聊天服务后,需要一个“服务角色”才能真正进行操作。在这个阶段,你开始使用“友好名称”


推荐阅读