Twilio invalid token issue

33 Views Asked by At

When I generate an access token for Twilio, it's generate but when I submit the access token to initialize device it's through me an error (mentioned in below). I'm trying my best to fix this but I can't, please give me a solution if you know that how to fix the issue. I'm using Twilio PHP SDK and JavaScript SDK for server-side and client-side.

[TwilioVoice][Device] Received error: AccessTokenInvalid: AccessTokenInvalid (20101): Twilio was unable to validate your Access Token

My JS code is:

initializeDevice() {
    this.device = new Device(this.token, {
        logLevel: 1,
        codecPreferences: ['opus', 'pcmu'],
        maxCallSignalingTimeoutMs: 30000
    });

    this.device.register();
}

My PHP code is:

public function accessToken()
{
    $identity = Str::random(5);

    // Twilio credentials
    $account_sid = env('TWILIO_SID');
    $auth_token = env('TWILIO_AUTH_TOKEN');
    $api_key = env('TWILIO_API_KEY');
    $twiml_app_sid = env('TWILIO_TWIML_SID');

    $token = new AccessToken($account_sid, $api_key, $auth_token, 3600, $identity, 'us1');

    $voiceGrant = new VoiceGrant();
    $voiceGrant->setOutgoingApplicationSid($twiml_app_sid);
    $voiceGrant->setIncomingAllow(true);

    $token->addGrant($voiceGrant);

    // Set the identity for the token
    $token->setIdentity($identity);

    $JwtToken = $token->toJWT();

    // Return the token as a JSON response
    return response()->json([
        'identity' => $identity,
        'token' => $JwtToken
    ]);
}
0

There are 0 best solutions below