• Модуль: socialservices
  • Путь к файлу: ~/bitrix/modules/socialservices/classes/general/bitrix24net.php
  • Класс: CBitrix24NetOAuthInterface
  • Вызов: CBitrix24NetOAuthInterface::GetAccessToken
public function GetAccessToken($redirect_uri = '')
{
	if($this->code === false)
	{
		$token = $this->getStorageTokens();

		// getStorageTokens returns null for unauthorized user
		if(is_array($token))
		{
			$this->access_token = $token["OATOKEN"];
			$this->accessTokenExpires = $token["OATOKEN_EXPIRES"];
		}

		if($this->access_token && $this->checkAccessToken())
		{
			return true;
		}
		elseif(isset($token["REFRESH_TOKEN"]))
		{
			if($this->getNewAccessToken($token["REFRESH_TOKEN"], $token["USER_ID"], true))
			{
				return true;
			}
		}

		return false;
	}

	$http = new BitrixMainWebHttpClient(array(
		'socketTimeout' => $this->httpTimeout,
		'streamTimeout' => $this->httpTimeout,
	));

	$result = $http->get($this->networkNode . self::TOKEN_URL . '?' . http_build_query([
		'code' => $this->code,
		'client_id' => $this->appID,
		'client_secret' => $this->appSecret,
		'redirect_uri' => $redirect_uri,
		'scope' => implode(',',$this->getScope()),
		'grant_type' => 'authorization_code',
	]));

	try
	{
		$arResult = Json::decode($result);
	}
	catch(BitrixMainArgumentException $e)
	{
		$arResult = array("error" => "ERROR_RESPONSE", "error_description" => "Wrong response from Network");
	}

	if(isset($arResult["access_token"]) && $arResult["access_token"] <> '')
	{
		if(isset($arResult["refresh_token"]) && $arResult["refresh_token"] <> '')
		{
			$this->refresh_token = $arResult["refresh_token"];
		}

		$this->access_token = $arResult["access_token"];
		$this->accessTokenExpires = time() + $arResult["expires_in"];

		$this->lastAuth = $arResult;

		return true;
	}
	return false;
}