0
votes

I am using a PHP server to generate token. When I initialize the chat client with the token generated, I am getting this error.

POST https://ems.us1.twilio.com/v1/token 401 (Unauthorized)

{"message":"The authorization with Token failed","code":2016,"user_error":false,"params":{"description":"Could not authenticate fpa token"}}

EMS E: Error: The authorization with Token failed

Javascript (v2.1) initilization code:

    Twilio.Chat.Client.create(token).then(client => {
        chatClient = client;
        chatClient.getChannelByUniqueName(channelName)
        .then(function(chosenChannel) {
            myChannel=chosenChannel;

PHP Server code:

    $token = new AccessToken( $accountid, $apikey, $apisecret 3600, 
    $identity);


    $chatGrant = new ChatGrant();
    $chatGrant->setServiceSid($configs[1]);
    $chatGrant->setEndpointId($endpointId);

    $token->addGrant($chatGrant);

    return $token->toJWT(); 

Any help?

2
I checked the token with jwt.io. The keys and ids are correct in the token. - arun kumar
In one of the solution, it was mentioned to remove the sendEndpointId in token generation. I tried that also. Still it gives the same error. - arun kumar

2 Answers

0
votes

left colon in $token = new AccessToken( $accountid, $apikey, $apisecret ##HERE## 3600, $identity);

0
votes

You are missing a colon in between the BOLD text below.

$token = new AccessToken( $accountid, $apikey, $apisecret 3600, $identity);