Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Ambersport\Strava;
- class OAuth extends \CSocServOAuthTransport
- {
- const SERVICE_ID = "Strava";
- const AUTH_URL = "https://www.strava.com/oauth/authorize";
- const TOKEN_URL = "https://www.strava.com/oauth/token";
- const ATHLETE_URL = "https://www.strava.com/api/v3/athlete";
- protected $userID = false;
- protected $userEmail = false;
- protected $scope = array(
- "email",
- );
- public function __construct($appID = false, $appSecret = false, $code = false)
- {
- if ($appID === false)
- {
- $appID = trim(\Ambersport\Strava\Auth::GetOption("client_id"));
- }
- if ($appSecret === false)
- {
- $appSecret = trim(\Ambersport\Strava\Auth::GetOption("client_secret"));
- }
- parent::__construct($appID, $appSecret, $code);
- }
- public function GetAuthUrl($redirect_uri, $state = '')
- {
- return self::AUTH_URL .
- "?client_id=" . urlencode($this->appID) .
- "&redirect_uri=" . urlencode($redirect_uri) .
- "&scope=" . $this->getScopeEncode() .
- "&response_type=code" .
- ($state <> '' ? '&state=' . urlencode($state) : '');
- }
- public function GetAccessToken($redirect_uri)
- {
- $token = $this->getStorageTokens();
- if (is_array($token))
- {
- $this->access_token = $token["OATOKEN"];
- return true;
- }
- if ($this->code === false)
- {
- return false;
- }
- $query = array(
- "client_id" => $this->appID,
- "client_secret" => $this->appSecret,
- "code" => $this->code,
- "redirect_uri" => $redirect_uri,
- );
- $httpClient = new \Bitrix\Main\Web\HttpClient(array(
- "socketTimeout" => $this->httpTimeout,
- "streamTimeout" => $this->httpTimeout,
- ));
- $result = $httpClient->post(self::TOKEN_URL, $query);
- try
- {
- $arResult = \Bitrix\Main\Web\Json::decode($result);
- } catch (\Bitrix\Main\ArgumentException $e)
- {
- $arResult = array();
- }
- if ((isset($arResult["access_token"]) && $arResult["access_token"] <> '') && isset($arResult['athlete']["id"]) && $arResult['athlete']["id"] <> '')
- {
- $this->access_token = $arResult["access_token"];
- $this->userID = $arResult['athlete']["id"];
- $this->userEmail = $arResult['athlete']["email"];
- $_SESSION["OAUTH_DATA"] = array("OATOKEN" => $this->access_token);
- return true;
- }
- return false;
- }
- public function GetCurrentUser()
- {
- if ($this->access_token === false)
- {
- return false;
- }
- $httpClient = new \Bitrix\Main\Web\HttpClient(array(
- "socketTimeout" => $this->httpTimeout,
- "streamTimeout" => $this->httpTimeout,
- ));
- $result = $httpClient->get(self::ATHLETE_URL . '?&access_token=' . urlencode($this->access_token));
- try
- {
- $result = \Bitrix\Main\Web\Json::decode($result);
- } catch (\Bitrix\Main\ArgumentException $e)
- {
- $result = array();
- }
- return $result;
- }
- public function GetCurrentUserEmail()
- {
- return $this->userEmail;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement