/** * Tries to refresh an access token if an refresh token is available. * Returns the new received access token or throws an excepion * * @param \PSX\Oauth2\AccessToken $accessToken * @return \PSX\Oauth2\AccessToken */ public function refreshToken(AccessToken $accessToken) { // request data $refreshToken = $accessToken->getRefreshToken(); $scope = $accessToken->getScope(); if (empty($refreshToken)) { throw new Exception('No refresh token was set'); } $data = array('grant_type' => 'refresh_token', 'refresh_token' => $refreshToken); if (!empty($scope)) { $data['scope'] = $scope; } // authentication $header = array(); if ($this->type == self::AUTH_BASIC) { $header['Authorization'] = 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret); } if ($this->type == self::AUTH_POST) { $data['client_id'] = $this->clientId; $data['client_secret'] = $this->clientSecret; } $request = new PostRequest($this->url, $header, $data); $response = $this->http->request($request); if ($response->getStatusCode() == 200) { return $this->importer->import($this->createAccessToken(), $response); } else { throw new Exception('Could not refresh access token'); } }