public static function register() { $httpClient = new HttpClient(); require_once $_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/classes/general/update_client.php"; $queryParams = array("action" => "register", "key" => md5(\CUpdateClient::GetLicenseKey()), "redirect_uri" => static::getRedirectUri()); $result = $httpClient->post(static::SERVICE_URL . static::REGISTER, $queryParams); $result = Json::decode($result); if ($result["error"]) { throw new SystemException($result["error"]); } else { static::setAccessSettings($result); } }
public function GetAppInfo() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $h->setTimeout($this->httpTimeout); $result = $h->post(static::TOKENINFO_URL . '?access_token=' . urlencode($this->access_token)); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result) && $result["audience"]) { $result["id"] = $result["audience"]; } return $result; }
protected function getLocationForResumableUpload(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('name', 'mimeType', 'size'))) { return null; } $accessToken = $this->getAccessToken(); $fileName = $fileData->getName(); $fileName = $this->convertToUtf8($fileName); $http = new HttpClient(array('redirect' => false, 'socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $http->setHeader('Content-Type', 'application/json; charset=UTF-8'); $http->setHeader('Authorization', "Bearer {$accessToken}"); $http->setHeader('X-Upload-Content-Type', $fileData->getMimeType()); $http->setHeader('X-Upload-Content-Length', $fileData->getSize()); $postFields = "{\"title\":\"{$fileName}\"}"; if ($http->post('https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&convert=' . ($fileData->isNeedConvert() ? 'true' : 'false'), $postFields) === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_GET_LOCATION_FOR_UPLOAD))); return null; } if (!$this->checkHttpResponse($http)) { return null; } return $http->getHeaders()->get('Location'); }
public function finance_query($method, $masterToken, $operationNum, $param = array(), $skipRefreshAuth = false) { if ($this->engineSettings['AUTH']) { $http = new HttpClient(); $http->setRedirect(false); $http->setHeader("Content-Type", "application/json; charset=utf-8"); $auth = $this->getCurrentUser(); $financeToken = hash("sha256", $masterToken . $operationNum . $method . $auth['login']); $postData = array("method" => $method, "finance_token" => $financeToken, "operation_num" => $operationNum, "locale" => $this->locale, "token" => $this->engineSettings['AUTH']['access_token']); if (!empty($param)) { $postData["param"] = $param; } $postData = YandexJson::encode($postData, JSON_UNESCAPED_UNICODE); $http->post(self::API_URL, $postData); if ($http->getStatus() == 401 && !$skipRefreshAuth) { if ($this->checkAuthExpired(false)) { $this->query($method, $param, true); } } return $http; } else { throw new SystemException("No Yandex auth data"); } }
/** * Returns HttpClient object with query result * * @param string $scope Url to call * @param string $method HTTP method (GET/POST/PUT supported) * @param array|null $data Post data * @param bool $skipRefreshAuth Skip authorization refresh * * @returns \Bitrix\Main\Web\HttpClient * @throws SystemException */ protected function query($scope, $method = "GET", $data = null, $skipRefreshAuth = false) { if ($this->engineSettings['AUTH']) { $http = new Web\HttpClient(); $http->setHeader('Authorization', 'OAuth ' . $this->engineSettings['AUTH']['access_token']); $http->setRedirect(false); switch ($method) { case 'GET': $http->get($scope); break; case 'POST': $http->post($scope, $data); break; case 'PUT': $http->query($method, $scope, $data); break; case 'DELETE': break; } if ($http->getStatus() == 401 && !$skipRefreshAuth) { if ($this->checkAuthExpired(false)) { $this->query($scope, $method, $data, true); } } return $http; } else { throw new SystemException("No Yandex auth data"); } }
public function getNewAccessToken($refreshToken = false, $userId = 0, $save = false, $scope = array()) { if ($this->appID == false || $this->appSecret == false) { return false; } if ($refreshToken == false) { $refreshToken = $this->refresh_token; } $http = new HttpClient(array('socketTimeout' => $this->httpTimeout)); $result = $http->post(static::TOKEN_URL, array('client_id' => $this->appID, 'client_secret' => $this->appSecret, 'refresh_token' => $refreshToken, 'grant_type' => 'refresh_token')); $arResult = Json::decode($result); if (isset($arResult["access_token"]) && $arResult["access_token"] != '') { $this->access_token = $arResult["access_token"]; $this->accessTokenExpires = time() + $arResult["expires_in"]; $this->refresh_token = $arResult["refresh_token"]; if ($save && intval($userId) > 0) { $dbSocservUser = CSocServAuthDB::GetList(array(), array("USER_ID" => intval($userId), "EXTERNAL_AUTH_ID" => CSocServBoxAuth::ID), false, false, array("ID")); $arOauth = $dbSocservUser->Fetch(); if ($arOauth) { CSocServAuthDB::Update($arOauth["ID"], array("OATOKEN" => $this->access_token, "OATOKEN_EXPIRES" => $this->accessTokenExpires, "REFRESH_TOKEN" => $this->refresh_token)); } } return true; } return false; }
public function call($methodName, $additionalParams = null) { global $APPLICATION; if (!$this->access_token) { $interface = Service::getEngine()->getInterface(); if (!$interface->checkAccessToken()) { if ($interface->getNewAccessToken()) { Service::getEngine()->setAuthSettings($interface->getResult()); } else { return $interface->getResult(); } } $this->access_token = $interface->getToken(); } if ($this->access_token) { if (!is_array($additionalParams)) { $additionalParams = array(); } else { $additionalParams = $APPLICATION->ConvertCharsetArray($additionalParams, LANG_CHARSET, "utf-8"); } $additionalParams['auth'] = $this->access_token; $http = new HttpClient(array('socketTimeout' => $this->httpTimeout)); $result = $http->post(CBitrixSeoOAuthInterface::URL . self::SERVICE_URL . $methodName, $additionalParams); /* AddMessage2Log(array( CBitrixSeoOAuthInterface::URL.self::SERVICE_URL.$methodName, $additionalParams, $http->getStatus(), $result, ));*/ $res = $this->prepareAnswer($result); if (!$res) { AddMessage2Log('Strange answer from Seo! ' . $http->getStatus() . ' ' . $result); } return $res; } else { throw new SystemException("No access token"); } }
public function sendMessage($uid, $message) { if ($this->access_token === false) { return false; } $url = self::GRAPH_URL . '/' . $uid . '/apprequests'; $message = CharsetConverter::ConvertCharset($message, LANG_CHARSET, "utf-8"); $arPost = array("access_token" => $this->access_token, "message" => $message); $ob = new HttpClient(); return $ob->post($url, $arPost); }