使用JS SDK设置Twilio IP消息传递

时间:2016-11-22 21:21:10

标签: twilio twilio-api

我在使用Twilio设置IPMessaging服务时遇到了一些麻烦。

我已经为用户身份验证设置了后端(PHP)。我可以使用Twillio Library生成令牌。

我还设置了与用户相关的API,以便从后端创建/更新/删除聊天服务的用户。我不得不用curl做这件事,因为即使我通过composer下载了PHP SDK并简单地复制/粘贴了文档示例,库仍然会返回致命错误。

我终于达到了可以通过后端创建用户的程度,然后使用ServiceSId,用户IdentitydeviceId创建令牌。

在JS客户端,再次无法设置服务,因为跟随文档导致多个错误。

我首先包含了来自Twilio CDN的JS SDK(与文档相同)。

然后我尝试初始化客户端:

var token = gottenFromOurAPI;

var chatClient = new Twilio.Chat.Client(token);
chatClient.initiazlie()
  .then(testchat);

function testchat(){ };

(如下所述:https://www.twilio.com/docs/api/chat/guides/initializing-sdk-clients

我在控制台中得到了这个:

angular.js:13920 chatClient: t {_events: Object, _maxListeners: undefined, _config: e, _channelsPromise: Promise, _channels: t…}
angular.js:13920 TypeError: chatClient.getChannels is not a function
    at testchat (supportchat.controller.js:33)
    at processQueue (angular.js:16383)
    at angular.js:16399
    at Scope.$eval (angular.js:17682)
    at Scope.$digest (angular.js:17495)
    at Scope.$apply (angular.js:17790)
    at done (angular.js:11831)
    at completeRequest (angular.js:12033)
    at XMLHttpRequest.requestLoaded (angular.js:11966)(anonymous function) @ angular.js:13920(anonymous function) @ angular.js:10467processQueue @ angular.js:16391(anonymous function) @ angular.js:16399$eval @ angular.js:17682$digest @ angular.js:17495$apply @ angular.js:17790done @ angular.js:11831completeRequest @ angular.js:12033requestLoaded @ angular.js:11966
twilio-chat.min.js:246 POST https://cds.twilio.com/v3/Lists 401 (Unauthorized)(anonymous function) @ twilio-chat.min.js:246t @ twilio-chat.min.js:196s @ twilio-chat.min.js:246p.post @ twilio-chat.min.js:246f.post @ twilio-chat.min.js:246value @ twilio-chat.min.js:245value @ twilio-chat.min.js:244(anonymous function) @ twilio-chat.min.js:244
twilio-chat.min.js:150 Chat E: Failed to create session Objectbody: Objectdescription: "Unauthorized"headers: ObjectContent-Type: "application/json"Twilio-Request-Id: "RQb761bc6d95034e23b4ba244e965334d4"__proto__: Objectstatus: 401__proto__: Objectvalue @ twilio-chat.min.js:150(anonymous function) @ twilio-chat.min.js:150
:3000/#/dashboard:1 Uncaught (in promise) Object {status: 401, description: "Unauthorized", headers: Object, body: Object}
:3000/#/dashboard:1 Uncaught (in promise) Objectbody: Objectdescription: "Unauthorized"headers: Objectstatus: 401__proto__: Object
:3000/#/dashboard:1 Uncaught (in promise) Objectbody: Objectdescription: "Unauthorized"headers: Objectstatus: 401__proto__: Object

因为我只是设置了这么多麻烦,还有关于替代(更好)服务的任何建议?

编辑 - 用于创作的PHP代码

    $endpointId = $this->idService . ':' . $this->idUser . ':' . $this->idDevice;

    // Create access token, which we will serialize and send to the client
    $token = new AccessToken(
            TWILIO_ACCOUNT_SID,
            TWILIO_API_KEY,
            TWILIO_API_SECRET,
            3600,
            $this->idUser
            );

    // Grant access to IP Messaging
    $grant = new IpMessagingGrant();
    $grant->setServiceSid($TWILIO_IPM_SERVICE_SID);
    $grant->setEndpointId($endpointId);
    $token->addGrant($grant);

    // return serialized token and the user's randomly generated ID
    $tokenArr = [
            'identity' => $this->idUser,
            'token' => $token->toJWT(),
    ];

    return $tokenArr;

0 个答案:

没有答案