javascript Twilio 聊天 SDK 上的身份访问被禁止

jon*_*iel 4 twilio

我正在使用 twilio chat javascript SDK,当我尝试创建新频道并邀请用户时遇到问题。两个用户(发送者和接收者)都收到此错误:错误:身份访问被禁止

但是,如果我转到仪表板,则频道是由 2 个成员创建的。我究竟做错了什么?

PS:我正在创建 Twilio API 上列出的用户令牌,我认为令牌不会成为问题,因为消息已经发送。当我加入频道时,问题似乎发生了。

我创建频道的代码:

this.client.createChannel({
  uniqueName: roomName,
  friendlyName: 'My Channel',
  type: 'private'
}).then(channel => {
  this.channel = channel
  this.channel.join()
});
Run Code Online (Sandbox Code Playgroud)

我的邀请码只是:

this.channel.invite(user)
Run Code Online (Sandbox Code Playgroud)

并生成用户令牌:

new Fingerprint2().get(fingerprint => {
  this.fingerprint = fingerprint

  let AUTH_TOKEN = $('meta[name=csrf-token]').attr('content')

  fetch('/chat/tokens', {
    method: 'POST',
    body: JSON.stringify({
      fingerprint: fingerprint,
      authenticity_token: AUTH_TOKEN,
      email: email
    }),
    headers: {
      'X-Requested-With': 'XMLHttpRequest',
      'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content'),
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
  }).then(result => result.json()).then(data => {
    callback({ token: data.token, username: data.username })
  })
})
Run Code Online (Sandbox Code Playgroud)

在我的 API 上

user = User.find_by_email(params[:email])

account_sid = ENV['TWILIO_ACCOUNT_SID']
api_key = 'SKe9fcdbefe0bdc1f01af4aa50d3548b70'
api_secret = 'oslALMC18tCZrUhBRDPin5KbqPSR9Rr4'

service_sid = ENV['TWILIO_SERVICE_ID']
device_id = params[:fingerprint]
identity = user.username
endpoint_id = "FakeEndPoint:#{identity}:#{device_id}"

grant = Twilio::JWT::AccessToken::IpMessagingGrant.new
grant.service_sid = service_sid
grant.endpoint_id = endpoint_id

token = Twilio::JWT::AccessToken.new(
  account_sid,
  api_key,
  api_secret,
  [grant],
  identity: identity
)

render status: 200, json: { token: token.to_jwt, username: user.username }
Run Code Online (Sandbox Code Playgroud)

令牌样本:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2U5ZmNkYmVmZTBiZGMxZjAxYWY0YWE1MGQzNTQ4YjcwLTE1MDExNjA0MjAiLCJncmFudHMiOnsiaWRlbnRpdHkiOiJqb25hdGFzIiwiaXBfbWVzc2FnaW5nIjp7InNlcnZpY2Vfc2lkIjoiSVMwYThiM2NkYTllMTU0YTUyOTg3MjJkOTRjOTI5ZjBhOSIsImVuZHBvaW50X2lkIjoiSGlwRmxvd1NsYWNrRG9ja1JDOmpvbmF0YXM6ZmU2NGZjYTA5NDc4YjYzNjNlYTFiMzA3OGQzOTQwM2MifX0sImlzcyI6IlNLZTlmY2RiZWZlMGJkYzFmMDFhZjRhYTUwZDM1NDhiNzAiLCJuYmYiOjE1MDExNjA0MjAsImV4cCI6MTUwMTE2NDAyMCwic3ViIjoiQUMxN2VmODM5N2JhODJkZWQ2ZDlmZmE0ODFkMWI2YTczMSJ9.UF8XtcEBN8LSCKVvBRscu9CmYdgMVobTd84RowF5KaU
Run Code Online (Sandbox Code Playgroud)

phi*_*ash 6

Twilio 开发人员布道者在这里。

当您加入频道(使用channel.join())时,承诺会在加入频道的请求成功时解决。这并不意味着频道已经完全加入。

相反,您应该channelJoined在聊天客户端上监听事件。一旦触发,您就可以确定您现在是该频道的成员并可以与之互动。

所以,如果你加入一个频道,你应该像这样监听事件:

this.client.on('channelJoined', function(channel) {
  console.log('Joined channel ' + channel.friendlyName) 
})

this.client.createChannel({
  uniqueName: roomName,
  friendlyName: 'My Channel',
  type: 'private'
}).then(channel => {
  this.channel = channel
  this.channel.join()
});
Run Code Online (Sandbox Code Playgroud)