Django / React JS中的Twilio TaskRouter

问题描述

我正在使用Twilio构建一个简单的联络中心应用程序。 我正在后端中创建TaskRouter Worker功能令牌,如下所示: 序列化器:

class TwilioTokenSerializer(serializers.BaseSerializer):
    def to_representation(self,instance):
        return {
            'token': instance,}

查看:

class TwilioWorkerView(generics.RetrieveAPIView):
    serializer_class = TwilioTokenSerializer
    def get_object(self):
        current_user = self.request.user.worker
        worker_sid   = current_user.worker_sid
        # Returns a Twilio Worker token for current agent
        capability = WorkerCapabilityToken(
            account_sid=TWILIO_ACCOUNT_SID,auth_token=TWILIO_AUTH_TOKEN,workspace_sid=TWILIO_WORKSPACE_SID,worker_sid=worker_sid
        )

        capability.allow_fetch_subresources()
        capability.allow_update_activities()
        capability.allow_update_reservations()
        token = capability.to_jwt()

        token = capability.to_jwt(ttl=28800)
        return token

反应中:

import { Worker } from 'twilio-taskrouter';

componentDidMount(){
  axios.get('https://myURL.com/api/twilio-worker-token')
    .then(res =>{
        console.log(res.data.token)
        const worker = new Worker(res.data.token)
    }).catch(error =>{
      console.log(error)
    })

但是我收到403 Web套接错误

index.window.js:24 WebSocket connection to 'wss://event-bridge.twilio.com/v1/wschannels?token=(here the token)&closeExistingSessions=false&clientVersion=0.5.2' Failed: Error during WebSocket handshake: Unexpected response code: 403
createWebSocket @ index.window.js:24
(anonymous) @ index.window.js:24
index.js:1 WebSocket error occurred:  Event {isTrusted: true,type: "error",target: WebSocket,currentTarget: WebSocket,eventPhase: 2, …}
console.<computed> @ index.js:1
<computed> @ index.window.js:17
webSocket.onerror @ index.window.js:24
error (async)
createWebSocket @ index.window.js:24
(anonymous) @ index.window.js:24
setTimeout (async)
webSocket.onclose @ index.window.js:24
index.window.js:17 Uncaught n {_errorData: {…},name: "GATEWAY_CONNECTION_Failed",message: "Could not connect to Twilio's servers."}
r.emit @ index.window.js:17
(anonymous) @ index.window.js:17
r.emit @ index.window.js:17
webSocket.onerror @ index.window.js:24
error (async)
createWebSocket @ index.window.js:24
(anonymous) @ index.window.js:24
setTimeout (async)
webSocket.onclose @ index.window.js:24
index.js:1 Uncaught Error: The error you provided does not contain a stack trace.
    at S (index.js:1)
    at V (index.js:1)
    at index.js:1
    at index.js:1
    at u (index.js:1)

相同的设置可以与带有taskrouter的cdn的html一起很好地工作 我在使用cdn和react twilio-taskrouter库时的区别是Web套接字url,而后者则既不包含帐户SID,也不包含工作程序SID。

我想念什么?

解决方法

我明白了,我在 twilio-taskrouter库中使用了错误的令牌,我应该在访问令牌中使用 TaskRouter Grant (而不是使用 TaskRouter Worker功能令牌)。

class TwilioWorkerView(generics.RetrieveAPIView):
    serializer_class = TwilioTokenSerializer
    def get_object(self):
        current_user   = self.request.user
        username = current_user.username
        agent_sid      = current_user.agent.agent_sid
        access = AccessToken(TWILIO_ACCOUNT_SID,TWILIO_API_KEY,TWILIO_API_SECRET)
        taskrouter_grant = TaskRouterGrant(workspace_sid=TWILIO_WORKSPACE_SID,worker_sid=agent_sid,role='worker')
        access.add_grant(taskrouter_grant)
        access.identity = username
        token = access.to_jwt()
        return token

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...