微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Oauth 2 $client-> getAccessToken()返回Null值

我需要你的帮助.我正在使用codeigniter MVC框架使用谷歌客户端库登录我的网站.当谷歌重定向代码并且我执行以下代码时,每件事都工作正常,期望$client-> getAccesstoken(). $client-> getAccesstoken()返回null值.我看了很多教程,但没有得到任何帮助.这是我的控制器功能代码之一.在此功能中,我设置我的凭据以创建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);
}

之后谷歌验证并重定向到我的重定向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没有返回任何东西和谷歌抛出预期.请帮帮我.

解决方法

看起来你永远不会得到刷新令牌.有两种不同的令牌,访问令牌每隔几个小时就会过期,但刷新令牌仅在重定向要求用户许可时才发送一次.它需要存储在安全的地方,并在将来用于刷新访问令牌.
以下是我的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);
            }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐