MethodNotAllowedException Laravel密码授予API

时间:2018-08-05 01:31:19

标签: php laravel oauth-2.0 guzzle laravel-passport

我最近开发了一个 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状态

我有两个问题要问你-

  1. 如何摆脱MethodNotAllowedHttpException错误?
  2. 我将访问令牌传递给请求的方式是否正确?如果没有,我该怎么办? 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中   堆栈跟踪:

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

  2.   
  3. Illuminate \ Routing \ RouteCollection-> methodNotAllowed()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:242

  4.   
  5. Illuminate \ Routing \ RouteCollection-> getRouteForMethods()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:176

  6.   
  7. Illuminate \ Routing \ RouteCollection-> match()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php:612

  8.   
  9. Illuminate \ Routing \ Router-> findRoute()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php:601

  10.   
  11. Illuminate \ Routing \ Router-> dispatchToRoute()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php:590

  12.   
  13. Illuminate \ Routing \ Router-> dispatch()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Kernel.php:176

  14.   
  15. Illuminate \ Foundation \ Http \内核-> Illuminate \ Foundation \ Http {closure}()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Pipeline。 php:30

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

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

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

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

  23.   
  24. Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149

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

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

  29.   
  30. Illuminate \ Foundation \ Http \ Middleware \ TransformsRequest-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149

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

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

  35.   
  36. Illuminate \ Foundation \ Http \ Middleware \ ValidatePostSize-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149

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

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

  41.   
  42. Illuminate \ Foundation \ Http \ Middleware \ CheckForMaintenanceMode-> handle()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Pipeline \ Pipeline.php:149

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

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

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

  49.   
  50. Illuminate \ Foundation \ Http \ Kernel-> sendRequestThroughRouter()C:\ xampp \ htdocs \ xampp \ tcc \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Kernel.php:116

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

  53.   

1 个答案:

答案 0 :(得分:1)

  

如何摆脱MethodNotAllowedHttpException错误?

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

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

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

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

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