guzzle NULL响应lumen php oauth2

时间:2015-09-24 05:58:32

标签: php laravel oauth-2.0 guzzle lumen

我已将this用于流明oauth2.0。

我遵循了所有步骤。 但是我收到了一个错误。我从guzzle得到了NULL响应。请检查下面的proxy.php中的代码。

namespace App\Auth;
use GuzzleHttp\Client;
class Proxy {
public function attemptLogin($credentials)
{
    return $this->proxy('password', $credentials);
}
public function attemptRefresh()
{
    $crypt = app()->make('encrypter');
    $request = app()->make('request');
    return $this->proxy('refresh_token', [
        'refresh_token' => $crypt->decrypt($request->cookie('refreshToken'))
    ]);
}
private function proxy($grantType, array $data = [])
{
    try {
        $config = app()->make('config');
        $data = array_merge([
            'client_id'     => $config->get('secrets.client_id'),
            'client_secret' => $config->get('secrets.client_secret'),
            'grant_type'    => $grantType
        ], $data);
        $client = new Client();
        $guzzleResponse = $client->post(sprintf('%s/oauth/access-token', $config->get('app.url')), [
            'form_params' => $data
        ]);
    } catch(\GuzzleHttp\Exception\BadResponseException $e) {
        $guzzleResponse = $e->getResponse();
    }
    $response = json_decode($guzzleResponse->getBody());
    if (property_exists($response, "access_token")) {
        $cookie = app()->make('cookie');
        $crypt  = app()->make('encrypter');
        $encryptedToken = $crypt->encrypt($response->refresh_token);
        // Set the refresh token as an encrypted HttpOnly cookie
        $cookie->queue('refreshToken',
            $encryptedToken,
            604800, // expiration, should be moved to a config file
            null,
            null,
            false,
            true // HttpOnly
        );
        $response = [
            'accessToken'            => $response->access_token,
            'accessTokenExpiration'  => $response->expires_in
        ];
    }
    $response = response()->json($response);
    $response->setStatusCode($guzzleResponse->getStatusCode());
    $headers = $guzzleResponse->getHeaders();
    foreach($headers as $headerType => $headerValue) {
        $response->header($headerType, $headerValue);
    }
    return $response;
}
}

在上面的代码guzzle post请求给我NULL响应。检查' $ response'来自代理功能。

我已经搜索过了。 本文作者提到

  1. 确保已安装memcached。
  2. 检查lumen / config / app.php中的网址

    return [
      'url' => 'http://localhost/lumen/lumen/public/',
      'key' => 'U<CdJu~T&.g/kR-NX55h]HfB+bb,b7Y*',
      'cipher' => 'AES-256-CBC'
     ];
    
  3. 检查.env文件并更改AUTH_MODEL = App \ Auth \ User

     APP_ENV=local
     APP_DEBUG=true
     APP_KEY=12asgvgjuiklp008765434d
    
     APP_LOCALE=en
     APP_FALLBACK_LOCALE=en
    
     DB_CONNECTION=mysql
     DB_HOST=localhost
     DB_PORT=3306
     DB_DATABASE=lumen
     DB_USERNAME=root
     DB_PASSWORD=
    
     AUTH_MODEL=App\Auth\User
    
     CACHE_DRIVER=memcached
     SESSION_DRIVER=memcached
     QUEUE_DRIVER=database
    
     # MAIL_DRIVER=smtp
     # MAIL_HOST=mailtrap.io
     # MAIL_PORT=2525
     # MAIL_USERNAME=null
     # MAIL_PASSWORD=null
     # MAIL_FROM_ADDRESS=null
     # MAIL_FROM_NAME=null
    
     # FILESYSTEM_DRIVER=local
     # FILESYSTEM_CLOUD=s3
    
     # S3_KEY=null
     # S3_SECRET=null
     # S3_REGION=null
     # S3_BUCKET=null
    
     # RACKSPACE_USERNAME=null
     # RACKSPACE_KEY=null
     # RACKSPACE_CONTAINER=null
     # RACKSPACE_REGION=null
    
  4. 一切都已完成。我仍然收到错误。

1 个答案:

答案 0 :(得分:1)

有时guzzle不适用于端口

如果您的服务器在http://localhost:8080上运行 刚删除端口8080并在http://localhost上运行 它将开始工作。