Skip to content

Instantly share code, notes, and snippets.

@Soliman
Last active February 28, 2021 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Soliman/49346134f1ffb7c6f7f6 to your computer and use it in GitHub Desktop.
Save Soliman/49346134f1ffb7c6f7f6 to your computer and use it in GitHub Desktop.
Google oAuth 2.0 PHP Curl Boilerplate
<?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