/** * @return object */ public function authenticate($key = null, $secret = null) { if (!is_null($key) && !is_null($secret)) { $this->key = $key; $this->secret = $secret; } $basic = $this->makeAuthorizationBasic($this->key, $this->secret); $token = $this->tokenRepository->findOneBy(['oauthToken' => $this->key]); if (is_null($token)) { $response = $this->postOauth2Token($basic); if (array_key_exists('errors', $response)) { throw new \Exception($response['errors'][0]['message']); } else { $token = $this->tokenRepository->persistBearerToken($this->key, $response['access_token']); } } return ['consumer_key' => $this->key, 'access_token' => $token->getOauthTokenSecret()]; }