php - guzzle NULL响应lumen php oauth2

标签 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中的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
    

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

最佳答案

有时 guzzle 不适用于端口

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

关于php - guzzle NULL响应lumen php oauth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754349/

相关文章:

java - Oauth2 和 Spring-Security : java. lang.IllegalStateException : No WebApplicationContext found: no ContextLoaderListener registered?

php - 如何在 PHP 中查看图像并更新

PHP 数组到带有 JSON 的 jQuery 数组。 ($.post、parseJSON、json_encode)

php - Laravel 5.8.* Eloquent 查询(使用 LeftJoin)返回不正确的关系结果

php - npm install etimed out 错误

Azure DevOps REST API SendMail

go - hydra - 使用 curl 创建策略失败,范围无效

php - 如何通过 id 从 html 元素中提取文本并分配给 php 变量?

php - PHP 脚本中未输出 XML

php - Laravel Auth::attempt() 返回 false