Oauth 2 $client->getAccessToken() Returns Null value Oauth 2 $client->getAccessToken() Returns Null value codeigniter codeigniter

Oauth 2 $client->getAccessToken() Returns Null value


It looks like you never get the refresh token. There are two different tokens, the access token expires every few hours or so, but the refresh token is only sent the one time when the redirect asks the user for permission. It needs to be stored somewhere secure and is used in the future to refresh the access token.Here's what my codeigniter code looks like to access the Google API (this would replace your if statements in the loginGoogle function:

        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);            }


If you are running on PLESK you might want to change the permission on /var/lib/php/session to 1777.

chmod 1777 /var/lib/php/sessions