| | |
| | | |
| | | protected function refreshAccessToken(OAuthCredentials $credentials): array |
| | | { |
| | | if (!$this->getClientID() || !$this->getClientSecret()) |
| | | if (!$this->getClientID() || !$this->getClientSecret() || empty($credentials->refresh_token)) { |
| | | $this->logError('refreshAccessToken', 'Missing client credentials or refresh token'); |
| | | return $this->response(false, 'No client ID or client secret provided'); |
| | | } |
| | | |
| | | $response = wp_remote_post( |
| | | $this->oauth->getToken(), |
| | | [ |
| | | 'timeout' => 15, |
| | | 'body' => [ |
| | | 'client_id' => $this->getClientID(), |
| | | 'client_secret' => $this->getClientSecret(), |
| | |
| | | } |
| | | |
| | | $data = json_decode(wp_remote_retrieve_body($response)); |
| | | if (isset($data['access_token'])) { |
| | | if (empty($data['access_token'])) { |
| | | $this->logError('refreshAccessToken', 'No access_token in refresh response', ['body' => $data]); |
| | | return $this->response(false, 'No Access Token in Response'); |
| | | } |
| | | $credentials = [ |
| | | 'access_token' => $data['access_token'], |
| | | 'expires_at' => time() + ($data['expires_in'] ?? 2592000) // 30 days default |
| | | 'expires_at' => strtotime($data['expires_at'] ?? '') ?: (time() + 2592000), |
| | | 'refresh_token' => $data['refresh_token'] ?? $credentials->refresh_token, |
| | | ]; |
| | | |
| | | $this->saveCredentials($credentials); |
| | | |
| | | return $this->response(true, 'Successfully refreshed access token'); |
| | | } |
| | | return $this->response(false, 'Something went wrong that wasn\'t caught - could not refresh access token'); |
| | | } |
| | | |
| | | protected function getRequestHeaders(): array |
| | | { |