From e6f805f76a2981342441b00f6c5cf0ff7869c3c8 Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Fri, 1 Mar 2019 21:00:37 -0300 Subject: [PATCH] Now Oauth is in a separate file than the API itself --- index.php | 95 +++++++------------------------------------------------ oauth.php | 74 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 84 deletions(-) diff --git a/index.php b/index.php index 47f54d0..882455b 100644 --- a/index.php +++ b/index.php @@ -1,94 +1,21 @@ 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; -} +$resource = getResource($_SESSION['token']); +echo "
\nuserName: {$resource['user']['userName']}"; +echo "
\nName: {$resource['user']['firstName']}"; +echo "
\nLast name: {$resource['user']['lastName']}"; +echo "
\nEmail: {$resource['user']['email']}"; +echo "
\nEdition: {$resource['edition']['name']}"; +echo "
\nOrganization: {$resource['organization']['name']}"; // we can now use the access_token as much as we want to access protected resources function getResource($access_token) { - global $test_api_url; + $test_api_url = 'https://cloud.memsource.com/web/api2/v1/auth/whoAmI'; $header = ["Authorization: Bearer {$access_token}"]; diff --git a/oauth.php b/oauth.php index e69de29..0b26468 100644 --- a/oauth.php +++ b/oauth.php @@ -0,0 +1,74 @@ +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; +}