laravel passport oauth:客户端错误:`POST http://127.0.0.1:8000 / oauth / token`导致'400 Bad Request`响应:(

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel passport oauth:客户端错误:`POST http://127.0.0.1:8000 / oauth / token`导致'400 Bad Request`响应:(相关的知识,希望对你有一定的参考价值。

我在Laracast的本地主机窗口上复制了Taylor Otwell的前端/后端护照示例:后端运行127.0.0.1:8000,前端运行127.0.0.:8080当我尝试客户端api调用时 - 127.0.0.1:8080/回调我得到这个:

ClientException in RequestException.php line 111:
Client error: `POST http://127.0.0.1:8000/oauth/token` 
resulted in a `400 Bad Request` response:
{"error":"invalid_request","message":"The request is missing a required
 parameter, includes an invalid parameter value, (truncated...)

而这个细节:

at Client request('post','http://127.0.0.1:8000/oauth/token',
array('form_params' => array('grant_type' => 'authorization_code',
'client_id' => 3, 
'client_secret' =>'23GCROVkVMtQqKsJKmx2xPojiDU4TOOe3ZMvKvQh', 
'redirect_uri' =>'http://127.0.0.1:8080/callback', 'code' => null),
 'synchronous' =>true)) in Client.php line 87 

at Client->__call('post', array('http://127.0.0.1:8000/oauth/token',
 array('form_params' =>array('grant_type' => 'authorization_code',
'client_id' => 3, 
'client_secret' =>'23GCROVkVMtQqKsJKmx2xPojiDU4TOOe3ZMvKvQh',
'redirect_uri' => 'http://127.0.0.1:8080/callback', 'code' => null))))in web.php line 3

这是相关代码:

Route::get('/callback', function () {

    $query = http_build_query([
        'client_id' => 3,
        'redirect_uri' => 'http://127.0.0.1:8080/callback',
        'response_type' => 'code',
        'scope' => ''
    ]);

    return redirect('http://127.0.0.1:8000/oauth/authorize?'.$query);
});

Route::get('/callback', function (Request $request) {
$http = new GuzzleHttpClient;

$response = $http->post('http://127.0.0.1:8000/oauth/token',[
    'form_params' => [
        'grant_type' => 'authorization_code',
        'client_id' => 3,
        'client_secret' => '23GCROVkVMtQqKsJKmx2xPojiDU4TOOe3ZMvKvQh',
        'redirect_uri' => 'http://127.0.0.1:8080/callback',
        'code' => $request -> code,
    ],
]);

return json_decode((string) $response->getBody(),true);

感谢任何建议

(fyi,我在用户模型中设置了HasApiTokens以及所有其他要求。我想知道它是否与尝试在本地主机上运行laravel客户端和laravel后端有关)。

答案

似乎在oauth / authorize重定向之后发布的帖子有一些params丢失。通常是因为代码必须已过期。我建议再次进行整个登录 - 授权循环。

以上是关于laravel passport oauth:客户端错误:`POST http://127.0.0.1:8000 / oauth / token`导致'400 Bad Request`响应:(的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Passport 密码授予刷新令牌

Laravel OAuth2 Passport API,生成令牌但无法发出请求:“未经身份验证”

总是为我的 Laravel Passport 生成的 OAuth 2 令牌响应 401(未授权)

Laravel Passport:部署到 aws 后缺少密钥

从 Laravel Passport 的 oauth_access_tokens 表中的现有令牌创建访问令牌

Passport Laravel Exception file_put_contents(/secret-keys/oauth\oauth-public.key): 无法打开流