From 764cf872881a198fc8ab0af010de3d2fa96526d7 Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Wed, 27 Feb 2019 01:41:46 -0300 Subject: [PATCH] INIT --- .gitignore | 1 + config.ini.TEMPLATE | 12 +++++ index.php | 106 ++++++++++++++++++++++++++++++++++++++++++++ oauth.php | 0 4 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 config.ini.TEMPLATE create mode 100644 index.php create mode 100644 oauth.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3507a4c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.ini \ No newline at end of file diff --git a/config.ini.TEMPLATE b/config.ini.TEMPLATE new file mode 100644 index 0000000..a52f07d --- /dev/null +++ b/config.ini.TEMPLATE @@ -0,0 +1,12 @@ + +; \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..47f54d0 --- /dev/null +++ b/index.php @@ -0,0 +1,106 @@ +here, copy the code, and paste it into the box below.

"; +} + +// step I, J - turn the authorization code into an access token, etc. +function getAccessToken($authorization_code) +{ + global $token_url, $client_id, $client_secret, $callback_uri; + + $authorization = base64_encode("$client_id:$client_secret"); + $header = ["Authorization: Basic {$authorization}", 'Content-Type: application/x-www-form-urlencoded']; + $content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$callback_uri"; + + $curl = curl_init(); + curl_setopt_array($curl, [ + CURLOPT_URL => $token_url, + CURLOPT_HTTPHEADER => $header, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $content + ]); + $response = curl_exec($curl); + $objectResponse = json_decode($response); + curl_close($curl); + + if ($response === false) { + echo 'Failed'; + echo curl_error($curl); + echo 'Failed'; + } elseif (isset($objectResponse->error)) { + echo 'Error:
'; + echo $authorization_code; + echo $response; + } + + return $objectResponse->access_token; +} + +// we can now use the access_token as much as we want to access protected resources +function getResource($access_token) +{ + global $test_api_url; + + $header = ["Authorization: Bearer {$access_token}"]; + + $curl = curl_init(); + curl_setopt_array($curl, [ + CURLOPT_URL => $test_api_url, + CURLOPT_HTTPHEADER => $header, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_RETURNTRANSFER => true + ]); + $response = curl_exec($curl); + curl_close($curl); + + return json_decode($response, true); +} diff --git a/oauth.php b/oauth.php new file mode 100644 index 0000000..e69de29