SHARE
TWEET

AmbersportStravaOAuth

GetFocus Jan 3rd, 2018 (edited) 132 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Ambersport\Strava;
  4.  
  5. class OAuth extends \CSocServOAuthTransport
  6. {
  7.     const SERVICE_ID = "Strava";
  8.  
  9.     const AUTH_URL = "https://www.strava.com/oauth/authorize";
  10.     const TOKEN_URL = "https://www.strava.com/oauth/token";
  11.     const ATHLETE_URL = "https://www.strava.com/api/v3/athlete";
  12.  
  13.     protected $userID = false;
  14.     protected $userEmail = false;
  15.  
  16.     protected $scope = array(
  17.         "email",
  18.     );
  19.  
  20.     public function __construct($appID = false, $appSecret = false, $code = false)
  21.     {
  22.         if ($appID === false)
  23.         {
  24.             $appID = trim(\Ambersport\Strava\Auth::GetOption("client_id"));
  25.         }
  26.  
  27.         if ($appSecret === false)
  28.         {
  29.             $appSecret = trim(\Ambersport\Strava\Auth::GetOption("client_secret"));
  30.         }
  31.  
  32.         parent::__construct($appID, $appSecret, $code);
  33.     }
  34.  
  35.     public function GetAuthUrl($redirect_uri, $state = '')
  36.     {
  37.         return self::AUTH_URL .
  38.         "?client_id=" . urlencode($this->appID) .
  39.         "&redirect_uri=" . urlencode($redirect_uri) .
  40.         "&scope=" . $this->getScopeEncode() .
  41.         "&response_type=code" .
  42.         ($state <> '' ? '&state=' . urlencode($state) : '');
  43.     }
  44.  
  45.     public function GetAccessToken($redirect_uri)
  46.     {
  47.         $token = $this->getStorageTokens();
  48.         if (is_array($token))
  49.         {
  50.             $this->access_token = $token["OATOKEN"];
  51.  
  52.             return true;
  53.         }
  54.  
  55.         if ($this->code === false)
  56.         {
  57.             return false;
  58.         }
  59.  
  60.         $query = array(
  61.             "client_id" => $this->appID,
  62.             "client_secret" => $this->appSecret,
  63.             "code" => $this->code,
  64.             "redirect_uri" => $redirect_uri,
  65.         );
  66.  
  67.         $httpClient = new \Bitrix\Main\Web\HttpClient(array(
  68.             "socketTimeout" => $this->httpTimeout,
  69.             "streamTimeout" => $this->httpTimeout,
  70.         ));
  71.  
  72.         $result = $httpClient->post(self::TOKEN_URL, $query);
  73.  
  74.         try
  75.         {
  76.             $arResult = \Bitrix\Main\Web\Json::decode($result);
  77.         } catch (\Bitrix\Main\ArgumentException $e)
  78.         {
  79.             $arResult = array();
  80.         }
  81.  
  82.         if ((isset($arResult["access_token"]) && $arResult["access_token"] <> '') && isset($arResult['athlete']["id"]) && $arResult['athlete']["id"] <> '')
  83.         {
  84.             $this->access_token = $arResult["access_token"];
  85.             $this->userID = $arResult['athlete']["id"];
  86.             $this->userEmail = $arResult['athlete']["email"];
  87.  
  88.             $_SESSION["OAUTH_DATA"] = array("OATOKEN" => $this->access_token);
  89.  
  90.             return true;
  91.         }
  92.  
  93.         return false;
  94.     }
  95.  
  96.     public function GetCurrentUser()
  97.     {
  98.         if ($this->access_token === false)
  99.         {
  100.             return false;
  101.         }
  102.  
  103.         $httpClient = new \Bitrix\Main\Web\HttpClient(array(
  104.             "socketTimeout" => $this->httpTimeout,
  105.             "streamTimeout" => $this->httpTimeout,
  106.         ));
  107.  
  108.  
  109.         $result = $httpClient->get(self::ATHLETE_URL . '?&access_token=' . urlencode($this->access_token));
  110.  
  111.         try
  112.         {
  113.             $result = \Bitrix\Main\Web\Json::decode($result);
  114.         } catch (\Bitrix\Main\ArgumentException $e)
  115.         {
  116.             $result = array();
  117.         }
  118.  
  119.         return $result;
  120.     }
  121.  
  122.     public function GetCurrentUserEmail()
  123.     {
  124.         return $this->userEmail;
  125.     }
  126.  
  127. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top