/** * @param IApiConnection $connection * @param SecurityDefinition $definition * * @return IToken */ public function getToken(IApiConnection $connection, SecurityDefinition $definition) { if ($definition->getType() !== 'oauth2') { throw new \InvalidArgumentException('The security definition provided is not a valid oAuth2 definition'); } $params = []; $params['grant_type'] = $this->getGrantType(); $params['api_user'] = $this->getApiUser(); $params['api_key'] = $this->getApiSecret(); $request = new ApiRequest(); $request->setConnection($connection); $detail = new ApiRequestDetail(); $detail->setMethod('POST'); $detail->setUrl($definition->getTokenUrl()); $detail->setPostFields($params); $request->setRequestDetail($detail); $connection->load($request); $tokenResponse = $request->getDecodedResponse(); $token = new AccessToken(); $token->setToken(Objects::property($tokenResponse, 'access_token')); $token->setType(Objects::property($tokenResponse, 'token_type', 'Bearer')); $token->setExpirySeconds(Objects::property($tokenResponse, 'expires_in')); $token->setExpiryTime(Objects::property($tokenResponse, 'expiry_time')); $token->setUserId(Objects::property($tokenResponse, 'uid')); $token->setSessionSecret(Objects::property($tokenResponse, 'session_secret')); return $token; }