【问题标题】:Oauth 2 $client->getAccessToken() Returns Null valueOauth 2 $client->getAccessToken() 返回 Null 值
【发布时间】:2019-08-23 17:14:23
【问题描述】:

我需要您的帮助。我正在使用 codeigniter MVC 框架通过 google 客户端库登录我的网站。当谷歌使用代码重定向并且我执行以下代码时,一切都工作正常期望 $client->getAccessToken() 。 $client->getAccessToken() 返回空值。我看了很多教程,但没有得到任何帮助。这是我的控制器功能一的代码。在这个函数中,我设置我的凭据来创建 authUrl。

public function login()
{
    // Include two files from google-php-client library in controller
    include_once APPPATH . 'third_party/google-api-php-client/vendor/autoload.php';

    // Store values in variables from project created in Google Developer Console
    $client_id = 'XXXXXX';
    $client_secret = 'XXXXX';
    $redirect_uri = 'path/to/mysite/login/loginGoogle';
    $simple_api_key = 'XXXXXXX';

    // Create Client Request to access Google API
    $client = new Google_Client();
    $client->setApplicationName("mysite");
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->setDeveloperKey($simple_api_key);
    $client->addScope("https://www.googleapis.com/auth/userinfo.email");

    $authUrl = $client->createAuthUrl();
    $data['authUrl'] = $authUrl;

    $this->load->view('login',$data);
}

之后,当 google 验证并重定向到我的重定向 uri 时,这是下面给出的另一个控制器功能。问题出在这个函数上。

public function loginGoogle()
{
    // Include two files from google-php-client library in controller
    include_once APPPATH . 'third_party/google-api-php-client/vendor /autoload.php';
       $client_id = 'XXXXXX';
        $client_secret = 'XXXXX';
        $redirect_uri = 'path/to/mysite/login/loginGoogle';
        $simple_api_key = 'XXXXXXX';

    // Create Client Request to access Google API
    $client = new Google_Client();
    $client->setApplicationName("mysite");
    $client->setClientId($client_id);
    $client->setClientSecret($client_secret);
    $client->setRedirectUri($redirect_uri);
    $client->setDeveloperKey($simple_api_key);
    $client->addScope("https://www.googleapis.com/auth/userinfo.email");
$objOAuthService = new Google_Service_Oauth2($client);

    // Add Access Token to Session
    if(!isset($_SESSION['access_token'])){

        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $token = $client->getAccessToken();                 
            $_SESSION['access_token'] = $token;
            print_r($this -> session -> userdata());exit;
            header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
        }
    }
    // Set Access Token to make Request
    if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
        $client->setAccessToken($_SESSION['access_token']);
    }
    // Get User Data from Google and store them in $data
    if ($client->getAccessToken()) {
        $userData = $objOAuthService->userinfo->get();
        $data['userData'] = $userData;
        $_SESSION['access_token'] = $client->getAccessToken();
    }}

这里的第二个函数 getAccessToken 什么都不返回,谷歌抛出了期望。请帮帮我。

【问题讨论】:

  • 有趣的是,我在使用 Android 登录时遇到了同样的问题。它总是返回一个空令牌。
  • 当你按照 api 文档中提到的正确做每一件事时,你仍然会遇到这些问题,这非常令人恼火。希望能在这里得到一些帮助。

标签: php codeigniter oauth oauth-2.0 google-api


【解决方案1】:

您似乎从未获得刷新令牌。有两种不同的令牌,访问令牌每隔几个小时左右过期一次,但刷新令牌仅在重定向请求用户许可时发送一次。它需要存储在安全的地方,并在将来用于刷新访问令牌。 这是我的 codeigniter 代码访问 Google API 的样子(这将替换 loginGoogle 函数中的 if 语句:

        if($refresh_token_accessed_from_my_database) {
            //If session contains no valid Access token, get a new one
            if ($client->isAccessTokenExpired()) {
                $client->refreshToken($refresh_token_accessed_from_my_database);
            }
            //We have access token now, launch the service
            $this->service = new Google_Service_Calendar($client);
        }
        else {
            //User has never been authorized, so let's ask for the ok
            if (isset($_GET['code'])) {
                //Creates refresh and access tokens
                $credentials = $client->authenticate($_GET['code']);

                //Store refresh token for further use
                //I store mine in the DB, I've seen others store it in a file in a secure place on the server
                $refresh_token = $credentials['refresh_token'];
                //refresh_token->persist_somewhere()

                //Store the access token in the session so we can get it after
                //the callback redirect
                $_SESSION['access_token'] = $client->getAccessToken();
                $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
                header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
            }

            if (!isset($_SESSION['access_token'])) {
                $auth_url = $client->createAuthUrl();
                header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
            }

            if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
                $client->setAccessToken($_SESSION['access_token']);
                $this->service = new Google_Service_Calendar($client);
            }

【讨论】:

    【解决方案2】:

    如果您在 PLESK 上运行,您可能希望将 /var/lib/php/session 的权限更改为 1777

    chmod 1777 /var/lib/php/sessions
    

    【讨论】:

    • 将文件系统中的任何内容设置为 777 都是一个安全漏洞,除非您绝对确定完全不重要。会话不包括在其中。使用 777 解决权限问题是不好的做法。首先使用正确的用户和正确的组。
    猜你喜欢
    • 2021-08-12
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2019-12-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多