Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MethodNotAllowedException Laravel密码授予API_Php_Laravel_Oauth 2.0_Guzzle_Laravel Passport - Fatal编程技术网

Php MethodNotAllowedException Laravel密码授予API

Php MethodNotAllowedException Laravel密码授予API,php,laravel,oauth-2.0,guzzle,laravel-passport,Php,Laravel,Oauth 2.0,Guzzle,Laravel Passport,我最近开发了一个密码授予API,供我的客户端应用程序使用。在客户端获得授权后,它正在为用户成功生成访问令牌 我现在面临的问题是如何在每次请求时将访问令牌从客户端应用程序传递回Laravel?(正如passport中的标题所做的那样)我已经通过passport文档进行了LaravelAPI身份验证 我想做的事 我已经在postman上测试了API的工作流程。它工作得很好。但现在我正努力把一切都硬编码。因此,现在我正在尝试为拥有访问令牌的用户获取帖子。 api.php(路由文件) API/UserC

我最近开发了一个
密码授予API
,供我的客户端应用程序使用。在客户端获得授权后,它正在为用户成功生成访问令牌

我现在面临的问题是如何在每次请求时将访问令牌从客户端应用程序传递回Laravel?(正如passport中的标题所做的那样)我已经通过passport文档进行了LaravelAPI身份验证

我想做的事

我已经在postman上测试了API的工作流程。它工作得很好。但现在我正努力把一切都硬编码。因此,现在我正在尝试为拥有访问令牌的
用户获取帖子。

api.php(路由文件)

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
。我知道这是因为我将我的post请求重定向到控制器中的其他页面。网络选项卡显示302状态

我有两个问题要问你-

  • 如何消除
    MethodNotAllowedHttpException
    错误
  • 我将访问令牌传递给请求的方式正确吗?如果不是,我该怎么做
    我在互联网上找不到任何资源来指导我这方面的工作
  • 错误堆栈

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

  • Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException->()C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\illighted\Routing\routedCollection.php:255

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

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

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

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

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

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

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

  • Illumb\Routing\Pipeline->Illumb\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\illumb\Pipeline\Pipeline.php:149

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

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

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

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

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

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

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

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

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

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

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

  • Illumb\Foundation\Http\Middleware\CheckFormIntenanceMode->handle()C:\xampp\htdocs\xampp\tcc\vendor\laravel\framework\src\Illumb\Pipeline\Pipeline.php:149

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

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

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

  • 照亮\Foundation\Http\Kernel->sendRequestThroughRouter()C:\xamp
    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);
      }
    }