Forked from Soliman/Google_oAuth2_PHP_Curl_Boilerplate.php
Created
February 28, 2021 22:26
-
-
Save humbletiger/20b26c771fffc3fc1ca6f7b582cc68e1 to your computer and use it in GitHub Desktop.
Google oAuth 2.0 PHP Curl Boilerplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Google oAuth 2.0 PHP Curl boilerplate | |
* Google Tasks API example | |
* | |
* | |
* Settings | |
* | |
* scope Space-delimited set of permissions that the application requests. | |
* state Provides any state that might be useful to your application upon receipt of the response. | |
* redirect_uri One of the redirect_uri values listed for this project in the Developers Console. | |
* client_id The client ID you obtain from the Developers Console. | |
* client_secret The client secret obtained from the Developers Console. | |
* | |
* Documentation: https://developers.google.com/accounts/docs/OAuth2WebServer | |
* Developers Console: https://console.developers.google.com | |
* | |
*/ | |
$scope = "email profile https://www.googleapis.com/auth/tasks"; | |
$state = "auth_ok"; | |
$redirect_uri = "http://localhost/google-oauth-php/"; | |
$client_id = "YOUR_CLIENT_ID"; | |
$client_secret = "YOUR_CLIENT_SECRET"; | |
/* STEP 1 - Ask user to accept access */ | |
// If no state | |
if(!$_GET['state']){ | |
// Build URL | |
$auth_url = "https://accounts.google.com/o/oauth2/auth"; | |
$auth_url .= "?"; | |
$auth_url .= "scope=$scope&"; | |
$auth_url .= "state=$state&"; | |
$auth_url .= "redirect_uri=$redirect_uri&"; | |
$auth_url .= "response_type=code&"; | |
$auth_url .= "client_id=$client_id"; | |
echo "<a href='$auth_url'>$auth_url</a>"; | |
} | |
/* STEP 2 - Ask Google for token */ | |
// If state is auth_ok | |
if($_GET['state'] == 'auth_ok'){ | |
// Build curl parameter array | |
$params = array( | |
"code" => $_GET['code'], | |
"client_id" => $client_id, | |
"client_secret" => $client_secret, | |
"redirect_uri" => $redirect_uri, | |
"grant_type" => "authorization_code" | |
); | |
// Init curl | |
$ch = curl_init(); | |
// Set curl options | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token'); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
// Execute curl | |
$r = curl_exec($ch); | |
// Close connection | |
curl_close($ch); | |
// Decode json | |
$returned_items = json_decode($r, true); | |
// Token - Aww Yiss! | |
$access_token = $returned_items['access_token']; | |
/* STEP 3 - Use access token with a Google API like Tasks */ | |
// Use this url to get task LISTS (So, a list of tasks lists, not tasks ;-) ) | |
// Note: @me represents the current user | |
// Reference: https://developers.google.com/google-apps/tasks/v1/reference/ | |
$url = "https://www.googleapis.com/tasks/v1/users/@me/lists?access_token=$access_token"; | |
// Init, execute, close curl | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
$r = curl_exec($ch); | |
curl_close($ch); | |
// Decode json | |
$returned_items = json_decode($r,true); | |
// Get lists | |
$task_lists = $returned_items['items']; | |
// For each list | |
foreach($task_lists as $k1=>$v1){ | |
/* STEP 4 - Get tasks */ | |
// List ID | |
$list_id = $v1['id']; | |
// Use this url to get tasks from tasklist $list_id | |
$url = "https://www.googleapis.com/tasks/v1/lists/$list_id/tasks?access_token=$access_token"; | |
// Init, execute, close curl | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
$r = curl_exec($ch); | |
curl_close($ch); | |
// Decode json | |
$returned_items = json_decode($r,true); | |
// Get tasks | |
$tasks = $returned_items['items']; | |
// Echo each task to the browser | |
echo '<pre>'; | |
foreach ($tasks as $k2=>$v2) { | |
echo $v1['title'] . ' ' . $v2['updated'] . ' ' . $v2['title'] . ' ' . $v2['status'] . "\r\n" ; | |
} | |
echo '</pre>'; | |
} // end for each task list | |
} // end if state = auth_ok | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment