protected function getAccessToken2() { //get new access token regardless echo "\n>>>" . time2s() . "cl.getAccessToken2()\n"; $accessTokenResponse = $this->cw->post($this->getRootEndpoint()->authEndpoint->href, $this->getBasicAuthHeader(), $this->getRefreshTokenPostFields()); $this->charAccessToken = $accessTokenResponse->content->access_token; $this->charAccessTokenExpiry = time() + $accessTokenResponse->content->expires_in - 10; echo time2s() . "cl.getAccessToken2() renewed, expires in " . $accessTokenResponse->content->expires_in . ", new token " . $this->charAccessToken . "\n"; return $this->charAccessToken; }
/** * Returns an access token, requesting a new one if none available or expired. * This method is called for every request made. * * @return string */ protected function getAccessToken() { //TODO: support multiple scopes //if we don't have an access token, get one if (!isset($this->charAccessToken) or time() >= $this->charAccessTokenExpiry) { $accessTokenResponse = $this->cw->post($this->getRootEndpoint()->authEndpoint->href, $this->getBasicAuthHeader(), $this->getRefreshTokenPostFields()); $this->charAccessToken = $accessTokenResponse->content->access_token; //The access token response is cached for a slightly lower time then it's actual validity. This way we //can fetch a new one before we run into trouble. Do so just after it was removed from cache. $this->charAccessTokenExpiry = $accessTokenResponse->getCacheExpiry() + 1; } return $this->charAccessToken; }