/** * {@inheritDoc} * * @throws ForbiddenAccessException * @throws \InvalidArgumentException */ public function preSend(RequestInterface $request) { if (($oauth2Header = $request->getHeader('Authorization')) && strpos($oauth2Header, 'Bearer') !== false) { return; } if (false === array_key_exists('access_token', self::$config)) { try { $data = $this->getAccessToken(); self::$config['token_type'] = $data['token_type']; self::$config['access_token'] = $data['access_token']; } catch (HttpResponseException $e) { throw new ForbiddenAccessException("Can't fetch access_token.", 0, $e); } } $request->addHeader(sprintf('Authorization: %s %s', ucfirst(strtolower(self::$config['token_type'])), self::$config['access_token'])); }