Skip to content

Commit

Permalink
Merge pull request #89 from pokap/fix_oauth2_toornament
Browse files Browse the repository at this point in the history
[Toornament] fixed request to oauth2 toornament
  • Loading branch information
deStrO authored May 30, 2017
2 parents fea9561 + 8cfdf30 commit 2679672
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions lib/ToornamentAPI.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,8 @@ public function retrieveKey()
$folder = sfConfig::get("sf_app_base_cache_dir");
$filename = $folder . DIRECTORY_SEPARATOR . "toornament.json";
if (!file_exists($filename) || @filemtime($filename) + 60 * 60 * 24 < time()) {
$this->token = $this->get("oauth/v2/token", array(
"grant_type" => "client_credentials",
"client_id" => $this->clientId,
"client_secret" => $this->clientSecret
));
file_put_contents($filename, json_encode($this->token));
$this->token = $this->requestOAuth2();
file_put_contents($filename, $this->token);
} else {
$this->token = json_decode(file_get_contents($filename), true);
}
Expand Down Expand Up @@ -139,4 +135,28 @@ public function put($uri, $params = array(), $needOAuth = false)
throw new Exception("PUT {$this->baseUrl}$uri returned $httpcode");
}
}

private function requestOAuth2()
{
$ch = $this->prepare('oauth/v2/token', array(
'Content-Type: application/x-www-form-urlencoded'
));

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
"grant_type" => 'client_credentials',
"client_id" => $this->clientId,
"client_secret" => $this->clientSecret
)));

$result = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpcode == 200) {
return $result;
} else {
throw new Exception("{$this->baseUrl}oauth/v2/token returned $httpcode");
}
}
}

0 comments on commit 2679672

Please sign in to comment.