I am trying to implement twilio chat using laravel. I have follow tutorial and try the source from github, https://github.com/TwilioDevEd/twiliochat-laravel. Works well in desktop, but when moving to linux enviroment, it shows error:
(1/1) BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $accountSid ]] in class Twilio\Jwt\AccessToken
The linux environment is using Laravel/Lumen 5.8.4 Here's the steps I have done :
composer require twilio/sdk
in routes/web.php :
//twilio
$router->group(['prefix' => 'twilio'], function() use ($router){
$router->post('/token',['uses' => 'TwilioController@generate','as' => 'token-generate']);
});
in app/Http/Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Twilio\Jwt\AccessToken;
use Twilio\Jwt\Grants\ChatGrant;
class TwilioController extends Controller
{
public function generate(Request $request, AccessToken $accessToken, ChatGrant $chatGrant)
{
var_dump('test aja');
exit;
$appName = "allsmartchat";
$deviceId = $request->input("device");
$identity = $request->input("identity");
$TWILIO_CHAT_SERVICE_SID = 'IS****'; //my SID
$endpointId = $appName . ":" . $identity . ":" . $deviceId;
$accessToken->setIdentity($identity);
$chatGrant->setServiceSid($TWILIO_CHAT_SERVICE_SID);
$chatGrant->setEndpointId($endpointId);
$accessToken->addGrant($chatGrant);
$response = array(
'identity' => $identity,
'token' => $accessToken->toJWT()
);
return response()->json($response);
}
}
in app/Providers/TwilioAccessProvider.php :
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Twilio\Jwt\AccessToken;
class TwilioAccessTokenProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->bind(
AccessToken::class, function ($app) {
$TWILIO_ACCOUNT_SID = 'AC***';
$TWILIO_API_KEY = 'SK***';
$TWILIO_API_SECRET = '***';
$token = new AccessToken(
$TWILIO_ACCOUNT_SID,
$TWILIO_API_KEY,
$TWILIO_API_SECRET,
3600
);
return $token;
}
);
}
}
I use postman to get the token value, in localhost, it produces token :
{
"identity": null,
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2E4OGQ2ZjVjNTEyNTkzMWMxMmYxYmFiYmFiZWY4MTU0LTE1NjM0MzkzOTQiLCJpc3MiOiJTS2E4OGQ2ZjVjNTEyNTkzMWMxMmYxYmFiYmFiZWY4MTU0Iiwic3ViIjoiQUFBQUNjZjMyMTdkNWU3NTEwMzA4ZjM2ZDZlYzNhZmJkOGNiZCIsImV4cCI6MTU2MzQ0Mjk5NCwiZ3JhbnRzIjp7ImNoYXQiOnsic2VydmljZV9zaWQiOiJJU2JjM2Y3N2Y2MjEzYjQ3MzliZTBlYTJiOTRiZTc4OWRmIiwiZW5kcG9pbnRfaWQiOiJUd2lsaW9DaGF0OjoifX19.OuAjJs_KGz-J_WTPlrCfAV_bjKmKCvSOTzj_ZGU0mUA"
}
but in server, it shows error :
(1/1) BindingResolutionException
Unresolvable dependency resolving [Parameter #0 [ <required> $accountSid ]] in class Twilio\Jwt\AccessToken
in Container.php line 973
at Container->unresolvablePrimitive(object(ReflectionParameter))
in Container.php line 911
at Container->resolvePrimitive(object(ReflectionParameter))
in Container.php line 852
at Container->resolveDependencies(array(object(ReflectionParameter), object(ReflectionParameter), object(ReflectionParameter), object(ReflectionParameter), object(ReflectionParameter)))
in Container.php line 818
at Container->build('Twilio\\Jwt\\AccessToken')
in Container.php line 667
at Container->resolve('Twilio\\Jwt\\AccessToken', array())
in Container.php line 615
at Container->make('Twilio\\Jwt\\AccessToken', array())
in Application.php line 260
at Application->make('Twilio\\Jwt\\AccessToken')
in BoundMethod.php line 167
at BoundMethod::addDependencyForCallParameter(object(Application), object(ReflectionParameter), array(), array(object(Request)))
in BoundMethod.php line 121
at BoundMethod::getMethodDependencies(object(Application), array(object(TwilioController), 'generate'), array())
in BoundMethod.php line 32
at BoundMethod::Illuminate\Container\{closure}()
in BoundMethod.php line 90
at BoundMethod::callBoundMethod(object(Application), array(object(TwilioController), 'generate'), object(Closure))
in BoundMethod.php line 34
at BoundMethod::call(object(Application), array(object(TwilioController), 'generate'), array(), null)
in Container.php line 576
at Container->call(array(object(TwilioController), 'generate'), array())
in RoutesRequests.php line 376
at Application->callControllerCallable(array(object(TwilioController), 'generate'), array())
in RoutesRequests.php line 342
at Application->callLumenController(object(TwilioController), 'generate', array(true, array('uses' => 'App\\Http\\Controllers\\TwilioController@generate', 'as' => 'token-generate'), array()))
in RoutesRequests.php line 316
at Application->callControllerAction(array(true, array('uses' => 'App\\Http\\Controllers\\TwilioController@generate', 'as' => 'token-generate'), array()))
in RoutesRequests.php line 278
at Application->callActionOnArrayBasedRoute(array(true, array('uses' => 'App\\Http\\Controllers\\TwilioController@generate', 'as' => 'token-generate'), array()))
in RoutesRequests.php line 263
at Application->handleFoundRoute(array(true, array('uses' => 'App\\Http\\Controllers\\TwilioController@generate', 'as' => 'token-generate'), array()))
in RoutesRequests.php line 165
at Application->Laravel\Lumen\Concerns\{closure}(object(Request))
in RoutesRequests.php line 416
at Application->sendThroughPipeline(array(), object(Closure))
in RoutesRequests.php line 171
at Application->dispatch(null)
in RoutesRequests.php line 108
at Application->run()
in index.php line 28
solved,
you need to register service in bootstrap/app.php
$app->register(App\Providers\TwilioAccessTokenProvider::class);
$app->register(App\Providers\TwilioChatGrantProvider::class);
also in laravel, you need to register service in config/app.php
App\Providers\TwilioAccessTokenProvider::class,
App\Providers\TwilioChatGrantProvider::class,