【问题标题】:Cannot send request to oauth/token using Guzzle/Http or Curl from iOS无法使用来自 iOS 的 Guzzle/Http 或 Curl 向 oauth/token 发送请求
【发布时间】:2018-12-25 07:04:06
【问题描述】:

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

【问题讨论】:

    标签: laravel postman guzzle laravel-passport


    【解决方案1】:

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

    【讨论】:

    • 感谢回复
    猜你喜欢
    • 2017-10-05
    • 2018-02-07
    • 2016-05-12
    • 2020-07-14
    • 2015-01-18
    • 2011-09-18
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多