我最近开发了一个 password grant API
,供我的客户端应用程序使用。客户端被授权后,它将成功为用户生成访问令牌。
我现在面临的问题是如何随每个请求将访问令牌从客户端应用程序传递回Laravel? (如护照上的标头所做的那样),我已经通过护照文档进行了laravel API身份验证。
我要做什么
我已经在邮递员上测试了API的工作流程。运行正常。但是现在我正在尝试对所有内容进行硬编码。所以现在我正在尝试获取 users who are having the access tokens.
api.php(路由文件)
Route::post('login', 'API\UserController@userSignIn')->name('login');
Route::middleware('auth:api')->get('/posts', function() {
$accessToken = Cookie::get('accessToken');
$client = new \GuzzleHttp\Client;
$response = $client->request('GET', 'http://tcc.devp/api/posts', [
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer '.$accessToken,
],
]);
return $accessToken;
})->name('posts');
API / UserController.php
public function userSignIn(Request $request){
if(Auth::attempt(['email' => $request['email'], 'password' => $request['password']])){
$http = new \GuzzleHttp\Client;
$response = $http->post('http://tcc.devp/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'username' => $request['email'],
'password' => $request['password'],
'scope' => '*'
],
]);
$response_array = json_decode((string) $response->getBody()->getContents(), true);
$accessToken = $response_array['access_token'];
$refreshToken = $response_array['refresh_token'];
Cookie::queue('accessToken', $accessToken, 60);
Cookie::queue('refreshToken', $refreshToken, 60);
return redirect()->route('posts'); //MethodNotAllowedException due to this line
}
else{
return response()->json(['error'=>'Unauthorised'], 401);
}
}
在执行此操作时,出现此错误 MethodNotAllowedHttpException
。我知道这是因为我将发帖请求重定向到控制器中的其他页面。 网络标签显示302状态。
我有两个问题要问你-
MethodNotAllowedHttpException
错误?I have found no resources on the internet to guide me in this direction
错误堆栈
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException:在文件C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php中 堆栈跟踪:
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException->()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:255
Illuminate \ Routing \ RouteCollection-> methodNotAllowed()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:242
Illuminate \ Routing \ RouteCollection-> getRouteForMethods()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:176
Illuminate \ Routing \ RouteCollection-> match()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php:612
Illuminate \ Routing \ Router-> findRoute()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php:601
Illuminate \ Routing \ Router-> dispatchToRoute()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php:590
Illuminate \ Routing \ Router-> dispatch()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Kernel.php:176
Illuminate \ Foundation \ Http \内核-> Illuminate \ Foundation \ Http {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline。 php:30
Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ fideloper \ proxy \ src \ TrustProxies.php:56
< / li>Fideloper \ Proxy \ TrustProxies-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149
Illuminate \ Pipeline \ Pipeline-> Illuminate \ Pipeline {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline.php:53 < / p>
Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest。 php:30
Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149
Illuminate \ Pipeline \ Pipeline-> Illuminate \ Pipeline {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline.php:53 < / p>
Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest。 php:30
Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149
Illuminate \ Pipeline \ Pipeline-> Illuminate \ Pipeline {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline.php:53 < / p>
Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ ValidatePostSize。 php:27
Illuminate \ Foundation \ Http \ Middleware \ ValidatePostSize-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149
Illuminate \ Pipeline \ Pipeline-> Illuminate \ Pipeline {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline.php:53 < / p>
Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ CheckForMaintenanceMode。 php:46
Illuminate \ Foundation \ Http \ Middleware \ CheckForMaintenanceMode-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149
Illuminate \ Pipeline \ Pipeline-> Illuminate \ Pipeline {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline.php:53 < / p>
Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:102 < / p>
Illuminate \ Pipeline \ Pipeline-> then()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Kernel.php:151
Illuminate \ Foundation \ Http \ Kernel-> sendRequestThroughRouter()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Kernel.php:116
Illuminate \ Foundation \ Http \ Kernel-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ public \ index.php:55
答案 0 :(得分:1)
如何摆脱MethodNotAllowedHttpException错误?
如果从Laravel控制器返回重定向响应,则下一个请求始终是GET
请求。为避免出现MethodNotAllowedHttpException
错误:
GET
方法我将访问令牌传递给请求的方式是否正确?如果不 我怎样才能做到这一点?我在互联网上找不到任何资源可以指导 我朝这个方向
您发送访问令牌的方式将起作用。如果您可以处理访问令牌过期的情况,那就更好了。在这种情况下,服务器可以使用刷新令牌自动交换新的访问令牌。
我个人建议不要向客户端发送访问令牌和刷新令牌。我通常将这些令牌保存到数据库或服务器缓存中,使令牌对客户端隐藏。