无法使用 Guzzle/Http 或来自 iOS 的 Curl 向 oauth/token 发送请求

Cannot send request to oauth/token using Guzzle/Http or Curl from iOS

我正在使用此函数使用 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);
}

我可以通过将虚拟 DNS 添加到我的 Web 服务器来解决此错误。显然,连接到网络时,请求很混乱,无法识别它应该定向到的本地主机。您将 DNS 添加到项目的文件夹中。