【问题标题】:guzzle NULL response lumen php oauth2guzzle NULL 响应流明 php oauth2
【发布时间】:2015-12-21 15:26:28
【问题描述】:

我已经使用this 用于lumen 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 中的 url

    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
    

一切都已完成。我仍然收到错误消息。

【问题讨论】:

  • 我认为它可以在虚拟主机上运行,​​但您仍然可以尝试在 .env 文件中使用 SESSION_DRIVER=cookie 而不是 SESSION_DRIVER=memcached

标签: php laravel oauth-2.0 guzzle lumen


【解决方案1】:

有时 guzzle 不适用于端口

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2018-11-23
    • 2018-05-29
    • 2015-11-14
    • 2020-09-28
    相关资源
    最近更新 更多