我正在使用此函数使用Laravel Passport向oauth / token路径发送请求但是当我向Postman发送一个具有下面给出的代码的函数的请求时,它会在它到达那里时停留并且在我取消请求之前没有响应。我到处使用dd()函数来确定代码卡在哪里以及它的这一部分。
$response = $http->post(url('oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => $client->id,
'client_secret' => $client->secret,
'username' => $request->name,
'password' => $request->password,
'scope' => ''
]
]);
我用4台不同的电脑试过这个功能。其中2个拥有Vagrant并且在这些机器上工作得很好。但我无法使用Mac计算机运行此功能。
我尝试使用GuzzleHttp \ Client向不同的路径发送请求,它在Mac上运行。
我尝试使用Mac上的Postman向oauth / token路径发送请求,它再次起作用。
这是我的完整登录功能:
/*
* Sends a POST request to "/login" with these parameters to login:
* domain, name, password.
* It returns a token, save it somewhere locally to access other routes.
*/
public function login(Request $request){
$request->validate([
'domain' => 'required',
'name' => 'required',
'password' => 'required',
]);
/*
* Gets the domain information such as database name, database ip etc.
* Then, connects to database with these informations to check if user exist or not.
*/
$domain = Domain::where('DOMAIN', $request->domain)->first();
if(!$domain){
return response([
'status' => 'error',
'message' => 'The credentials do not match with our records'
], 400);
}
setDatabase($domain);
/*
* Checks the existence of the user
*/
$user = Kullanici::where('KULLANICI', $request->name)->first();
if(!$user || bcrypt($request->password) != $user->SIFRE){
return response([
'status' => 'error',
'message' => 'The credentials do not match with our records'
], 400);
}
/*
* Sends a request to given url for the access token. If successfull,
* returns the token
*/
$http = new Client;
$client = ClientApp::where('id',2)->first();
DB::setDefaultConnection('mysql');
$response = $http->post(url('oauth/token'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => $client->id,
'client_secret' => $client->secret,
'username' => $request->name,
'password' => $request->password,
'scope' => ''
]
]);
/*
* encode the domain information in JWT format and return this token also.
* This token gets decoded in every request and updates the database with this info.
* You can check the details in Domain Middleware
*/
$payload = [
"domain" => $request->domain
];
$jwt_domain = JWT::encode($payload);
return response([
'auth' => json_decode((string)$response->getBody(), true),
'domain' => $jwt_domain
], 200);
}
我可以通过向我的Web服务器添加虚拟DNS来解决此错误。显然,连接到网络时,请求会混淆,无法识别应该指向的本地主机。您将DNS添加到项目的文件夹中。