Laravel 5.4 Passport:unsupported_grant_type

时间:2017-07-27 09:45:11

标签: php angular laravel laravel-passport

您好我尝试做一个Web应用程序,但我遇到了问题。 当我从/ oauth / token请求令牌时,我收到了这个回复:

{"error":"unsupported_grant_type","message":"The authorization grant type is not supported by the authorization server.","hint":"Check the `grant_type` parameter"}

我的代码是:

import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class UserService {

    private API: string = 'api/';
    private OAuth: string = 'oauth/'

    constructor(private _HTTP: Http) { }

    private get_header(): RequestOptions {
        let headers = new Headers();
        headers.append('Access-Control-Allow-Origin', '*' ); 
        headers.append('Content-Type', 'application/x-www-form-urlencoded' );  
        return new RequestOptions({ headers: headers });
    }

    Signin(email: string, password: string): void {

        let data = {
            form_params: {
                grant_type: 'password',
                client_id: 2,
                client_secret: 'vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms',
                username: email,
                password: password,
                scope: '*',
            }
        };

        this._HTTP.post(
            this.OAuth + 'token',
            data,
            this.get_header()
        ).toPromise()
        .then( (_response) => {
           console.log (_response); 
        });
    }
}

请求标题:

POST /oauth/token HTTP/1.1
Host: localhost:8000
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8000/signin
Access-Control-Allow-Origin: *
Content-Type: application/json
X-XSRF-TOKEN: eyJpdiI6IkxtOHhqd0RDUW9MVjl1YVh0U0c4N2c9PSIsInZhbHVlIjoiUmdrenlLWll2eEFtdGFQK2dsWGN0Nm5jWGY2MW5HXC9zMDJFdU52SEh4RUoxSkY1QWVHUFNySXFkUUQ3SDNaTW0zNll6SVRONlFHQjBFVzZPT0RxQkR3PT0iLCJtYWMiOiIwNzg5ZjliMzUwYjE5ZWM4MWE3MTg3NDRjYWZiMDE1MWI1NWJjN2E1NmI5ZTMzY2UzMTIwODI4ODY0ZDQ1ZDY5In0=
Content-Length: 208
Cookie: XSRF-TOKEN=eyJpdiI6IkxtOHhqd0RDUW9MVjl1YVh0U0c4N2c9PSIsInZhbHVlIjoiUmdrenlLWll2eEFtdGFQK2dsWGN0Nm5jWGY2MW5HXC9zMDJFdU52SEh4RUoxSkY1QWVHUFNySXFkUUQ3SDNaTW0zNll6SVRONlFHQjBFVzZPT0RxQkR3PT0iLCJtYWMiOiIwNzg5ZjliMzUwYjE5ZWM4MWE3MTg3NDRjYWZiMDE1MWI1NWJjN2E1NmI5ZTMzY2UzMTIwODI4ODY0ZDQ1ZDY5In0%3D; laravel_session=eyJpdiI6ImlrdlNMTGtTK241WVArZGx6MzE5Mnc9PSIsInZhbHVlIjoiRVQxSmlpZFwvV3B4eVRHVUdVYlRtY1VOZHUzZ09FQnMyZjhjSnZoQjA0VVBvM0x5YnJJbmx3b25cL3dCbVZScTVUb2lTVkg5Sldyd3R0aFluMDBvcmhxQT09IiwibWFjIjoiZDk4NjZkMDhiNTE0NzA3YzQxODVkNGJjN2E3OTRjNWEzMjc2Njk2ZjEyODY2MzY3NmRhYzAzN2U1NGE0ZTg4NCJ9
Connection: keep-alive

响应标题:

HTTP/1.1 400 Bad Request
Host: localhost:8000
Connection: close
x-powered-by: PHP/7.1.1
Content-Type: application/json
Cache-Control: no-cache, private
Date: Thu, 27 Jul 2017 09:35:53 +0000, Thu, 27 Jul 2017 09:35:53 GMT
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59

发布数据:

{
  "form_params": {
    "grant_type": "password",
    "client_id": 2,
    "client_secret": "vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms",
    "username": "fasfa",
    "password": "fasfa",
    "scope": "*"
  }
}

我没有任何其他细节。

3 个答案:

答案 0 :(得分:6)

您需要为Passport安装password客户端。假设您已安装并配置了Passport,要生成密码客户端,请运行:

php artisan passport:client --password

确保在API请求中使用此命令输出中的详细信息。

答案 1 :(得分:0)

我在Fiddler上使用了以下请求,效果很好:enter image description here

答案 2 :(得分:0)

在邮递员中使用的表单数据如下

grant_type : password

client_id  :  2

client_secret : vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms or whatever this is 

username: myemail@yahoo.com 

password: mypassword

scope: 

如果那里没有数据,则可以使用以下命令创建客户端,从而从oauth_clients表中获取客户端ID和密码

php artisan passport:client --password

使用oauth_clients表的特定行,其中密码客户端列的值为1
看看我如何将令牌生成代码与登录集成在一起。我使用正确的格式来解决laravel 5.8中的拨款问题

public function login(Request $request) {
    if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
        $user = Auth::user();

         if(Auth::check()) {  // check whether user exist
           $req = Request::create('/oauth/token', 'POST',[
                     'grant_type' => 'password',
                     'client_id' => 2,
                     'client_secret' => 'WDv6wrY9Tf9HuixaiQjDWGy7yBqEG1IrmsLucAUI',
                     'username' => request('email'),
                     'password' => request('password'),
                     'scope' => '' 
                   ]);   
                   $res = app()->handle($req);
                   $responseBody = json_decode($res->getContent()); // convert to json object
           return response()->json(['success' => $responseBody], $res->getStatusCode());

         } else {
            return response()->json(['error' => 'Not logged in '], 401);
          }

    } else {
        return response()->json(['error' => 'Authentication failed '], 401);
    }
}