如何在Laravel 4.2中使用GuzzleHttp处理重定向URI?

时间:2019-07-25 13:21:49

标签: php laravel-4 guzzle

我正在尝试使用Laravel 4.2网站实现LiveChat integration。我的网站中包含许多项目,因此URL具有以下结构:https://mylaravelsite.com/admin/ {project_id} / ...

获取LiveChat授权令牌的GET请求必须类似于:

  

https://accounts.livechatinc.com/    ?response_type =令牌    &client_id = 2261a58dfe1420acc0dc1bd77158f7ac    &redirect_uri = https%3A%2F%2Fmy-application.com    &state = i8XNjC4b8KVok4uw5RftR38Wgp2BFwql

如您所见,存在“ redirect_uri”选项,其中仅允许使用静态URI。

我在/ admin / {project_id} / livechat_integration页面上进行授权。

主要内容部分是:

<div class="integration">
    <div class="integration-button">
        <button type="button" id="livechat_auth">Authorize</button>
    </div>
</div>

$("#livechat_auth").on("click", function(event){       
    $.ajax({
        type: "POST",
        url: './livechat_integration/request_code',
        dataType: "json",
        data: {
            project_id: project._project_id
        },
        success: function(data){
          console.log('data sent');
          console.log(data);
        },
        error: function(data){
          console.log(data);
        }
    });
});

然后是route.php:

Route::any('{p}/livechat_integration/request_code', [
'as' => 'LiveChat',
'uses' => 'App\Controllers\Webhook\LiveChatWebhookController@requestCode'
]);

和控制器功能:

    $projectID = \Request::get('project_id');
    $guzzleClient = new Client();

    try{
        $response = $guzzleClient->request('GET', self::LIVECHAT_ADDRESS, [
            'query' => [
                'response_type' => 'token',
                'client_id' => self::CLIENT_ID,
                'redirect_uri' => self::AUTH_LINK,
                'state' => '22hjdfuierhf84kfefefw'
            ]
        ]);
    } catch (GuzzleException $ex){
        return Response::json(['success' => 'error'], 401);
    }

    return Response::json(['success' => 'success', 'response' => $response->getBody()], 200);

结果是,响应是成功的,但是我显然没有得到任何结果,因为我在redirect_uri http://mylaravelsite.com/webhook/livechat中指定了它,它具有以下路由:

Route::any('/webhook/livechat', array ( 'as' => 'webhook_livechat', 'uses' => 'App\Controllers\Webhook\LiveChatWebhookController@receiveAuthData'));

receiveAuthData很简单(出于测试目的):

    \DB::table('testest')->insert(['test' => 'auth data received']);
    return \Redirect::to(\Auth::user()->project->projID . '/livechat_integration');

问题在于GuzzleHttp不会重定向到/ webhook / livechat页面,我从LiveChat接收不到任何内容。你能告诉我如何处理吗?

0 个答案:

没有答案