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

时间:2018-07-17 14:17:41

标签: laravel postman guzzle laravel-passport

我正在使用此函数使用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 Computers运行此功能。

我尝试使用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);
}

1 个答案:

答案 0 :(得分:1)

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