Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 Passport: unsupported_grant_type

Hello I try to do a web application and I have a problem. When I request the token from /oauth/token I receive this response:

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

And my code is:

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); 
        });
    }
}

And the request header:

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

Response header:

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

Post data:

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

I don't have any other detail.

like image 595
George Valentin Avatar asked Jul 27 '17 09:07

George Valentin


2 Answers

You need to install a password client for Passport. Assuming you've installed and configured Passport, to generate a password client, run:

php artisan passport:client --password

Make sure to use the details from the output of this command in your API requests.

like image 159
Aaron Fahey Avatar answered Nov 11 '22 13:11

Aaron Fahey


inside form-data in postman use following

grant_type : password

client_id  :  2

client_secret : vSFxVqALQHjyotPyGfhrGj3ziudUGsts2ZWiAGms or whatever this is 

username: [email protected] 

password: mypassword

scope: 

You can get client id and secret from oauth_clients table if no data there use following command to create a client

php artisan passport:client --password

use the oauth_clients table particular row, where password client column value is 1
see how i have integrated token generation code with login. I used right format to solve grant issue in 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);
    }
}
like image 40
Rajib Avatar answered Nov 11 '22 13:11

Rajib