MethodNotAllowedException Laravel密码授予API [英] MethodNotAllowedException Laravel Password Grant API

查看:110
本文介绍了MethodNotAllowedException Laravel密码授予API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开发了一个 password grant API ,供我的客户端应用程序使用.客户端被授权后,它将成功为用户生成访问令牌.

I have recently developed a password grant API to be used by my client application. It is successfully generating access tokens for users after the client has been authorized.

我现在面临的问题是如何随每个请求将访问令牌从客户端应用程序传递回Laravel? (如护照上的标头所做的那样),我已经通过了护照文件中的laravel API身份验证.

The problem I'm facing now is how to pass the access token back from client application to Laravel with each request? (as done by Headers in passport) I have gone through the laravel API Authentication by passport documentation.

我要做什么

我已经在邮递员上测试了API的工作流程.工作正常.但是现在我正在尝试对所有内容进行硬编码.所以现在我正在尝试获取 users who are having the access tokens.

I have already tested the work flow of the API on postman. It was working fine. But now I'm trying to hard code everything. SO now I'm trying to fetch posts for the 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状态.

While doing so, I'm getting this error MethodNotAllowedHttpException. I got to know that it is because I'm redirecting my post request to some other page in the controller. network tab is showing 302 status.

我有两个问题要问你-

  1. 如何摆脱MethodNotAllowedHttpException错误?
  2. 将访问令牌传递给请求的方式是否正确?如果没有,我该怎么办? I have found no resources on the internet to guide me in this direction
  1. How can I get rid of MethodNotAllowedHttpException error?
  2. Is my way of passing the access token to the request correct? If not how can I do that? I have found no resources on the internet to guide me in this direction

错误堆栈

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException:在255行的文件C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php中 堆栈跟踪:

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException: in file C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php on line 255 Stack trace:

  1. Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException->()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:255

  1. 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->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->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\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->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->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\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\Foundation\Http\Kernel->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

Illuminate\Routing\Pipeline->Illuminate\Routing{closure}() C:\xampp\htdocs\xampp\tcc\vendor\fideloper\proxy\src\TrustProxies.php:56

Fideloper \ Proxy \ TrustProxies-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149

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

Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53

Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest .php:30

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\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

Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53

Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest .php:30

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\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

Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53

Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ ValidatePostSize .php:27

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\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

Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53

Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ CheckForMaintenanceMode .php:46

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\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

Illuminate\Pipeline\Pipeline->Illuminate\Pipeline{closure}() C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Routing\Pipeline.php:53

Illuminate \ Routing \ Pipeline-> Illuminate \ Routing {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:102

Illuminate\Routing\Pipeline->Illuminate\Routing{closure}() C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php:102

Illuminate \ Pipeline \ Pipeline-> then()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Kernel.php:151

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->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

Illuminate\Foundation\Http\Kernel->handle() C:\xampp\htdocs\xampp\tcc\public\index.php:55

推荐答案

如何摆脱MethodNotAllowedHttpException错误?

How can I get rid of MethodNotAllowedHttpException error?

如果从Laravel控制器返回重定向响应,则下一个请求始终是GET请求.为避免MethodNotAllowedHttpException错误:

If you return a redirection response from a Laravel controller, the next request is always a GET request. To avoid the MethodNotAllowedHttpException error:

  • 确保已为重定向响应中使用的路径注册了GET方法
  • (或)返回一个JSON对象以告诉客户端将另一个请求发送到目标URI.

我将访问令牌传递给请求的方式是否正确?如果不 我怎样才能做到这一点?我在互联网上找不到任何可指导的资源 我朝着这个方向

Is my way of passing the access token to the request correct? If not how can I do that? I have found no resources on the internet to guide me in this direction

您发送访问令牌的方式将起作用.如果您可以处理访问令牌过期的情况,那就更好了.在这种情况下,服务器可以使用刷新令牌自动交换新的访问令牌.

Your way of sending access token will work. It will be better if you can handle the case when the access token expires. In that case, the server can automatically exchange a new access token by using the refresh token.

就个人而言,我建议不要向客户端发送访问令牌和刷新令牌.我通常将这些令牌保存到数据库或服务器缓存中,使令牌对客户端隐藏.

Personally, I recommend against sending access token and refresh token to the client. I usually save these tokens to databases or server caches, keeping tokens hidden from the client side.

这篇关于MethodNotAllowedException Laravel密码授予API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆