代码之家  ›  专栏  ›  技术社区  ›  jonatasdaniel

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

  •  4
  • jonatasdaniel  · 技术社区  · 8 年前

    我正在使用twilio聊天javascript SDK,当我尝试创建新频道并邀请用户时遇到了问题。 错误:身份禁止访问

    但如果我转到仪表板,通道是由2个成员创建的。

    PS:我正在创建TwilioAPI上列出的用户令牌,我认为令牌不会有问题,因为消息已经发送了。

    我创建频道的代码:

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

    this.channel.invite(user)
    

    并生成用户令牌:

    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 })
      })
    })
    

    在我的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 }
    

    eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2U5ZmNkYmVmZTBiZGMxZjAxYWY0YWE1MGQzNTQ4YjcwLTE1MDExNjA0MjAiLCJncmFudHMiOnsiaWRlbnRpdHkiOiJqb25hdGFzIiwiaXBfbWVzc2FnaW5nIjp7InNlcnZpY2Vfc2lkIjoiSVMwYThiM2NkYTllMTU0YTUyOTg3MjJkOTRjOTI5ZjBhOSIsImVuZHBvaW50X2lkIjoiSGlwRmxvd1NsYWNrRG9ja1JDOmpvbmF0YXM6ZmU2NGZjYTA5NDc4YjYzNjNlYTFiMzA3OGQzOTQwM2MifX0sImlzcyI6IlNLZTlmY2RiZWZlMGJkYzFmMDFhZjRhYTUwZDM1NDhiNzAiLCJuYmYiOjE1MDExNjA0MjAsImV4cCI6MTUwMTE2NDAyMCwic3ViIjoiQUMxN2VmODM5N2JhODJkZWQ2ZDlmZmE0ODFkMWI2YTczMSJ9.UF8XtcEBN8LSCKVvBRscu9CmYdgMVobTd84RowF5KaU
    
    1 回复  |  直到 8 年前
        1
  •  6
  •   philnash    8 年前

    Twilio开发者布道者。

    当你加入一个频道时 channel.join()

    相反,你应该倾听 channelJoined event on the chat client 一旦触发,你可以确定你现在是该频道的一员,并且可以与之互动。

    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()
    });