Gustavo Guimaraes
Gustavo Guimaraes

Reputation: 96

Twilio Programable Chat Ruby quickstart. Getting 401 errors

I am trying out the Twilio programable chat api via this repo: https://github.com/TwilioDevEd/ipm-quickstart-ruby

At the moment it is falling verifying the token that is produced in the server.

However I tried another chat demo written in node and it works just fine: https://github.com/twilio/twilio-chat-demo-js

My project is in Ruby. I am looking for any insight from someone from Twilio or the community here who has solved this problem and can point me in the right direction to have the app up and running.

Upvotes: 1

Views: 95

Answers (1)

Gustavo Guimaraes
Gustavo Guimaraes

Reputation: 96

This was a difficult problem to troubleshoot.

The client expects the push_credentials_sid key under the grant hash in the JWT token.

This is the code that solves the issue:

app.rb

# Generate a token for use in our IP Messaging application
get '/token' do
  # Get the user-provided ID for the connecting device
  device_id = params['device']

  # Create a random username for the client
  identity = Faker::Internet.user_name

  # Create a unique ID for the currently connecting device
  endpoint_id = "TwilioDemoApp:#{identity}:#{device_id}"

  # Create an Access Token for IP messaging usage
  token = Twilio::Util::AccessToken.new ENV['TWILIO_ACCOUNT_SID'],
    ENV['TWILIO_API_KEY'], ENV['TWILIO_API_SECRET'], 3600, identity

  # Create IP Messaging grant for our token
  grant = Twilio::Util::AccessToken::IpMessagingGrant.new
  grant.service_sid = ENV['TWILIO_IPM_SERVICE_SID']
  grant.push_credential_sid = 'CRe9c5eff29e744709d7df875f8a797bf0'
  grant.endpoint_id = endpoint_id
  token.add_grant grant

  # Generate the token and send to client
  json :identity => identity, :token => token.to_jwt
end

Upvotes: 2

Related Questions