/** * Get an OAuth2 subscriber to add to Guzzle clients. * * @throws \RuntimeException * * @return Oauth2Subscriber */ protected function getOauth2Plugin() { if (!$this->oauth2Plugin) { if (!$this->isLoggedIn()) { throw new \RuntimeException('Not logged in'); } $options = ['base_url' => $this->config['accounts'], 'defaults' => ['headers' => ['User-Agent' => $this->config['user_agent']], 'debug' => $this->config['debug'], 'verify' => $this->config['verify'], 'proxy' => $this->config['proxy']]]; $oauth2Client = $this->getOauth2Client($options); $refreshTokenGrantType = null; if ($this->session->get('refreshToken')) { $refreshTokenGrantType = new RefreshToken($oauth2Client, ['client_id' => $this->config['client_id'], 'client_secret' => $this->config['client_secret'], 'refresh_token' => $this->session->get('refreshToken'), 'token_url' => $this->config['token_url']]); } $this->oauth2Plugin = new Oauth2Subscriber(null, $refreshTokenGrantType); if ($this->session->get('accessToken')) { $type = $this->session->get('tokenType'); // If the token does not expire, the 'expires' time must be null. $expires = $this->session->get('expires') ?: null; $this->oauth2Plugin->setAccessToken($this->session->get('accessToken'), $type, $expires); } } return $this->oauth2Plugin; }