我正在使用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()
});
我的邀请码只是:
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
答案 0 :(得分:3)
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()
});