繁体   English   中英

Laravel 5.4 护照:unsupported_grant_type

[英]Laravel 5.4 Passport: unsupported_grant_type

您好,我尝试做一个 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": "*"
  }
}

我没有任何其他细节。

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

php artisan passport:client --password

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

我在Fiddler上使用了以下请求,它运行正常: 在此输入图像描述

内部表格 - 邮递员使用后的数据

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表特定行,其中password客户端列值为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);
    }
}

检查标题内容类型:

'内容类型':'应用程序/json'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM