我在Laracast的本地主机窗口上复制了Taylor Otwell的前端/后端护照示例:后端运行127.0.0.1:8000,前端运行127.0.0.:8080 当我尝试客户端api调用 - 127.0.0.1:8080/callback 时,我得到了这个:
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 GuzzleHttp\Client;
$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后端有关)
答案 0 :(得分:0)
似乎oauth / authorize重定向后的帖子有一些params丢失。通常是因为代码必须已过期。我建议再次进行整个登录 - 授权循环。