无法解析的依赖关系解析类 Twilio\Jwt\AccessToken 中的 [参数 #0 [ <required> $accountSid ]]

问题描述 投票:0回答:2
我正在尝试使用 laravel 实现 twilio 聊天。我已经按照教程并尝试了来自 github 的源代码,

https://github.com/TwilioDevEd/twiliochat-laravel。在桌面上运行良好,但是当移动到linux环境时,它显示错误:

(1/1) BindingResolutionException Unresolvable dependency resolving [Parameter #0 [ <required> $accountSid ]] in class Twilio\Jwt\AccessToken

linux环境使用的是Laravel/Lumen 5.8.4 这是我已完成的步骤:

作曲家需要 twilio/sdk

在routes/web.php中:

//twilio $router->group(['prefix' => 'twilio'], function() use ($router){ $router->post('/token',['uses' => 'TwilioController@generate','as' => 'token-generate']); });

在应用程序/Http/控制器中:

<?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); } }

在应用程序/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; } ); } }

我使用邮递员获取令牌值,在本地主机中,它生成令牌:

{ "identity": null, "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImN0eSI6InR3aWxpby1mcGE7dj0xIn0.eyJqdGkiOiJTS2E4OGQ2ZjVjNTEyNTkzMWMxMmYxYmFiYmFiZWY4MTU0LTE1NjM0MzkzOTQiLCJpc3MiOiJTS2E4OGQ2ZjVjNTEyNTkzMWMxMmYxYmFiYmFiZWY4MTU0Iiwic3ViIjoiQUFBQUNjZjMyMTdkNWU3NTEwMzA4ZjM2ZDZlYzNhZmJkOGNiZCIsImV4cCI6MTU2MzQ0Mjk5NCwiZ3JhbnRzIjp7ImNoYXQiOnsic2VydmljZV9zaWQiOiJJU2JjM2Y3N2Y2MjEzYjQ3MzliZTBlYTJiOTRiZTc4OWRmIiwiZW5kcG9pbnRfaWQiOiJUd2lsaW9DaGF0OjoifX19.OuAjJs_KGz-J_WTPlrCfAV_bjKmKCvSOTzj_ZGU0mUA" }

但是在服务器中,它显示错误:

(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
    
twilio twilio-php
2个回答
1
投票
上面的解决方案没问题,但您也可以在应用程序提供程序中添加此代码,它肯定会起作用 首先包含类

use Twilio\Jwt\AccessToken; $this->app->bind( AccessToken::class, function ($app) { $accountSid = config('services.twilio')['accountSid']; $apiKey = config('services.twilio')['apiKey']; $apiSecret = config('services.twilio')['apiSecret']; return new AccessToken($accountSid, $apiKey, $apiSecret, 3600, 'identity'); } );
    

0
投票
解决了,

您需要在 bootstrap/app.php 中注册服务

$app->register(App\Providers\TwilioAccessTokenProvider::class); $app->register(App\Providers\TwilioChatGrantProvider::class);

同样在laravel中,你需要在config/app.php中注册服务

App\Providers\TwilioAccessTokenProvider::class, App\Providers\TwilioChatGrantProvider::class,
    
© www.soinside.com 2019 - 2024. All rights reserved.