3

I am trying to integrate Twilio chat (message, voice, and video) in my application. I am not able to get chat client in the below code

 // Set up Twilio Chat client after getting token
    TwilioChatClient.chatClient(withToken: token, properties: nil, delegate: self) {(result, chatClient) in

    // Here chatClient is coming nil all the time and the result is not successful.

       self.client = chatClient

      }
  }
Abhishek
  • 358
  • 3
  • 10
  • Have you checked the `result` parameter and what it says? Is there an error message you are missing? – philnash Jun 02 '19 at 23:07
  • Error Domain=signal.sdk.domain.error Code=102 "Error validating token." UserInfo={kTCHErrorMsgKey=Error validating token., NSLocalizedDescription=Error validating token. – Abhishek Jun 03 '19 at 04:15
  • Cool, so that means that there is something wrong with the token you have generated. The issue isn't in your iOS app. So, what is the code you are using to generate the token? Can you check it looks like a valid token using https://jwt.io. – philnash Jun 03 '19 at 06:12
  • I am using Programmable Chat Token for generating the access token. It's saying invalid signature. – Abhishek Jun 03 '19 at 06:23
  • Yup, and where are you generating that token from? – philnash Jun 03 '19 at 06:23
  • In the example code, there is one method getting the token from the URL function name - retrieveToken Github Link: https://github.com/twilio/video-quickstart-swift – Abhishek Jun 03 '19 at 06:30
  • Did you generate an example token from the Twilio console? Or did you set up a server as described here? https://github.com/twilio/video-quickstart-swift#setup-an-access-token-server – philnash Jun 03 '19 at 06:33
  • I have used Twilio console to generate URL using Programmable Chat Token. Here is an example of that: https://********.twil.io/chat-token I am using this link for example project setup Github: https://github.com/TwilioDevEd/chat-quickstart-swift – Abhishek Jun 03 '19 at 06:38
  • Ok, you're using a Twilio Function to generate your token? Great. Can you share the code you are using in the Function? – philnash Jun 03 '19 at 06:39
  • Here it is: exports.handler = function(context, event, callback) { let appName = context.CHAT_APP_NAME; let identity = event.identity; let deviceId = event.device; let endpointId = `${appName}:${identity}:${deviceId}`; //pre-built in libraries to create a MessagingGrant let AccessToken = Twilio.jwt.AccessToken; let IpMessagingGrant = AccessToken.IpMessagingGrant; let ipmGrant = new IpMessagingGrant({ serviceSid: context.CHAT_SERVICE_SID, endpointId: endpointId, pushCredentialSid: context.CHAT_PUSH_CREDENTIAL_SID }); – Abhishek Jun 03 '19 at 06:46
  • const accessToken = new AccessToken( context.ACCOUNT_SID, context.CHAT_TWILIO_API_KEY, context.CHAT_TWILIO_API_SECRET ); accessToken.addGrant(ipmGrant); accessToken.identity = identity; callback(null, {token: accessToken.toJwt()}); }; Not able to add this in one comment. So, added seperately. – Abhishek Jun 03 '19 at 06:48

1 Answers1

2

Twilio developer evangelist here.

There seems to be something wrong with your token generation as you get an error when trying to instantiate the Chat client.

The code you're using to generate the token is mostly right, though a little out of date. We can use a ChatGrant now, rather than the IpMessagingGrant and there's no need to include an endpointId any more. I would update your function to:

exports.handler = function(context, event, callback) {
  let identity = event.identity;
  let AccessToken = Twilio.jwt.AccessToken;
  let ChatGrant = AccessToken.ChatGrant;
  let chatGrant = new ChatGrant({
    serviceSid: context.CHAT_SERVICE_SID,
    pushCredentialSid: context.CHAT_PUSH_CREDENTIAL_SID
  });
  const accessToken = new AccessToken(
    context.ACCOUNT_SID,
    context.CHAT_TWILIO_API_KEY,
    context.CHAT_TWILIO_API_SECRET
  );
  accessToken.addGrant(chatGrant);
  accessToken.identity = identity;
  callback(null, { token: accessToken.toJwt() });
};

Also, I would confirm that you have correctly set the following environment variables in the function configuration: CHAT_SERVICE_SID, CHAT_PUSH_CREDENTIAL_SID, ACCOUNT_SID, CHAT_TWILIO_API_KEY and CHAT_TWILIO_API_SECRET.

If any of those are missing or incorrect then your token will be invalid.

Let me know how that goes.

philnash
  • 70,667
  • 10
  • 60
  • 88