From 5c7f2f17f9c471d306955df457c7cab4e5c6ed3b Mon Sep 17 00:00:00 2001 From: Snap Date: Thu, 16 Apr 2015 14:51:26 -0700 Subject: Google's OpenID Connect method $google_client_id & $google_client_secret must be added to db.inc.php! --- .../examples/appengineauth.php | 46 +++++++ .../examples/batch.php | 82 +++++++++++++ .../examples/fileupload.php | 135 +++++++++++++++++++++ .../examples/idtoken.php | 107 ++++++++++++++++ .../examples/index.php | 19 +++ .../examples/multi-api.php | 114 +++++++++++++++++ .../examples/service-account.php | 89 ++++++++++++++ .../examples/simple-query.php | 87 +++++++++++++ .../examples/simplefileupload.php | 123 +++++++++++++++++++ .../examples/styles/style.css | 113 +++++++++++++++++ .../examples/templates/base.php | 90 ++++++++++++++ .../examples/user-example.php | 132 ++++++++++++++++++++ 12 files changed, 1137 insertions(+) create mode 100644 includes/google-api-php-client-master/examples/appengineauth.php create mode 100644 includes/google-api-php-client-master/examples/batch.php create mode 100644 includes/google-api-php-client-master/examples/fileupload.php create mode 100644 includes/google-api-php-client-master/examples/idtoken.php create mode 100644 includes/google-api-php-client-master/examples/index.php create mode 100644 includes/google-api-php-client-master/examples/multi-api.php create mode 100644 includes/google-api-php-client-master/examples/service-account.php create mode 100644 includes/google-api-php-client-master/examples/simple-query.php create mode 100644 includes/google-api-php-client-master/examples/simplefileupload.php create mode 100644 includes/google-api-php-client-master/examples/styles/style.css create mode 100644 includes/google-api-php-client-master/examples/templates/base.php create mode 100644 includes/google-api-php-client-master/examples/user-example.php (limited to 'includes/google-api-php-client-master/examples') diff --git a/includes/google-api-php-client-master/examples/appengineauth.php b/includes/google-api-php-client-master/examples/appengineauth.php new file mode 100644 index 0000000..7b147e9 --- /dev/null +++ b/includes/google-api-php-client-master/examples/appengineauth.php @@ -0,0 +1,46 @@ +setApplicationName("Client_Library_Examples"); + +$auth = new Google_Auth_AppIdentity($client); +$token = $auth->authenticateForScope(Google_Service_Storage::DEVSTORAGE_READ_ONLY); +if (!$token) { + die("Could not authenticate to AppIdentity service"); +} +$client->setAuth($auth); + +$service = new Google_Service_Storage($client); +$results = $service->buckets->listBuckets(str_replace("s~", "", $_SERVER['APPLICATION_ID'])); + +echo "

Results Of Call:

"; +echo "
";
+var_dump($results);
+echo "
"; + +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/batch.php b/includes/google-api-php-client-master/examples/batch.php new file mode 100644 index 0000000..3c8812f --- /dev/null +++ b/includes/google-api-php-client-master/examples/batch.php @@ -0,0 +1,82 @@ +setApplicationName("Client_Library_Examples"); +$apiKey = ""; // Change to your API key. +// Warn if the API key isn't changed! +if (strpos($apiKey, "<") !== false) { + echo missingApiKeyWarning(); + exit; +} else { + $client->setDeveloperKey($apiKey); + + $service = new Google_Service_Books($client); + + /************************************************ + To actually make the batch call we need to + enable batching on the client - this will apply + globally until we set it to false. This causes + call to the service methods to return the query + rather than immediately executing. + ************************************************/ + $client->setUseBatch(true); + + /************************************************ + We then create a batch, and add each query we + want to execute with keys of our choice - these + keys will be reflected in the returned array. + ************************************************/ + $batch = new Google_Http_Batch($client); + $optParams = array('filter' => 'free-ebooks'); + $req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams); + $batch->add($req1, "thoreau"); + $req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams); + $batch->add($req2, "shaw"); + + /************************************************ + Executing the batch will send all requests off + at once. + ************************************************/ + $results = $batch->execute(); + + echo "

Results Of Call 1:

"; + foreach ($results['response-thoreau'] as $item) { + echo $item['volumeInfo']['title'], "
\n"; + } + echo "

Results Of Call 2:

"; + foreach ($results['response-shaw'] as $item) { + echo $item['volumeInfo']['title'], "
\n"; + } +} + +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/fileupload.php b/includes/google-api-php-client-master/examples/fileupload.php new file mode 100644 index 0000000..03a928e --- /dev/null +++ b/includes/google-api-php-client-master/examples/fileupload.php @@ -0,0 +1,135 @@ +'; +$client_secret = ''; +$redirect_uri = ''; + +$client = new Google_Client(); +$client->setClientId($client_id); +$client->setClientSecret($client_secret); +$client->setRedirectUri($redirect_uri); +$client->addScope("https://www.googleapis.com/auth/drive"); +$service = new Google_Service_Drive($client); + +if (isset($_REQUEST['logout'])) { + unset($_SESSION['upload_token ']); +} + +if (isset($_GET['code'])) { + $client->authenticate($_GET['code']); + $_SESSION['upload_token'] = $client->getAccessToken(); + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); +} + +if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { + $client->setAccessToken($_SESSION['upload_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['upload_token']); + } +} else { + $authUrl = $client->createAuthUrl(); +} + +/************************************************ + If we're signed in then lets try to upload our + file. + ************************************************/ +if ($client->getAccessToken()) { + $file = new Google_Service_Drive_DriveFile(); + $file->title = "Big File"; + $chunkSizeBytes = 1 * 1024 * 1024; + + // Call the API with the media upload, defer so it doesn't immediately return. + $client->setDefer(true); + $request = $service->files->insert($file); + + // Create a media file upload to represent our upload process. + $media = new Google_Http_MediaFileUpload( + $client, + $request, + 'text/plain', + null, + true, + $chunkSizeBytes + ); + $media->setFileSize(filesize(TESTFILE)); + + // Upload the various chunks. $status will be false until the process is + // complete. + $status = false; + $handle = fopen(TESTFILE, "rb"); + while (!$status && !feof($handle)) { + $chunk = fread($handle, $chunkSizeBytes); + $status = $media->nextChunk($chunk); + } + + // The final value of $status will be the data from the API for the object + // that has been uploaded. + $result = false; + if ($status != false) { + $result = $status; + } + + fclose($handle); +} +echo pageHeader("File Upload - Uploading a large file"); +if (strpos($client_id, "googleusercontent") == false) { + echo missingClientSecretsWarning(); + exit; +} +?> +
+
+Connect Me!"; +} +?> +
+ +
+ +
+
+'; +$client_secret = ''; +$redirect_uri = ''; + +$client = new Google_Client(); +$client->setClientId($client_id); +$client->setClientSecret($client_secret); +$client->setRedirectUri($redirect_uri); +$client->setScopes('email'); + +/************************************************ + If we're logging out we just need to clear our + local access token in this case + ************************************************/ +if (isset($_REQUEST['logout'])) { + unset($_SESSION['access_token']); +} + +/************************************************ + If we have a code back from the OAuth 2.0 flow, + we need to exchange that with the authenticate() + function. We store the resultant access token + bundle in the session, and redirect to ourself. + ************************************************/ +if (isset($_GET['code'])) { + $client->authenticate($_GET['code']); + $_SESSION['access_token'] = $client->getAccessToken(); + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); +} + +/************************************************ + If we have an access token, we can make + requests, else we generate an authentication URL. + ************************************************/ +if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { + $client->setAccessToken($_SESSION['access_token']); +} else { + $authUrl = $client->createAuthUrl(); +} + +/************************************************ + If we're signed in we can go ahead and retrieve + the ID token, which is part of the bundle of + data that is exchange in the authenticate step + - we only need to do a network call if we have + to retrieve the Google certificate to verify it, + and that can be cached. + ************************************************/ +if ($client->getAccessToken()) { + $_SESSION['access_token'] = $client->getAccessToken(); + $token_data = $client->verifyIdToken()->getAttributes(); +} + +echo pageHeader("User Query - Retrieving An Id Token"); +if (strpos($client_id, "googleusercontent") == false) { + echo missingClientSecretsWarning(); + exit; +} +?> +
+
+Connect Me!"; +} else { + echo "Logout"; +} +?> +
+ +
+ +
+
+ + +'; + $client_secret = ''; + $redirect_uri = ''; + +/************************************************ + Make an API request on behalf of a user. In + this case we need to have a valid OAuth 2.0 + token for the user, so we need to send them + through a login flow. To do this we need some + information from our API console project. + ************************************************/ +$client = new Google_Client(); +$client->setClientId($client_id); +$client->setClientSecret($client_secret); +$client->setRedirectUri($redirect_uri); +$client->addScope("https://www.googleapis.com/auth/drive"); +$client->addScope("https://www.googleapis.com/auth/youtube"); + +/************************************************ + We are going to create both YouTube and Drive + services, and query both. + ************************************************/ +$yt_service = new Google_Service_YouTube($client); +$dr_service = new Google_Service_Drive($client); + + +/************************************************ + Boilerplate auth management - see + user-example.php for details. + ************************************************/ +if (isset($_REQUEST['logout'])) { + unset($_SESSION['access_token']); +} +if (isset($_GET['code'])) { + $client->authenticate($_GET['code']); + $_SESSION['access_token'] = $client->getAccessToken(); + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); +} + +if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { + $client->setAccessToken($_SESSION['access_token']); +} else { + $authUrl = $client->createAuthUrl(); +} + +/************************************************ + If we're signed in, retrieve channels from YouTube + and a list of files from Drive. + ************************************************/ +if ($client->getAccessToken()) { + $_SESSION['access_token'] = $client->getAccessToken(); + + $dr_results = $dr_service->files->listFiles(array('maxResults' => 10)); + + $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true)); + $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes; + $yt_results = $yt_service->playlistItems->listPlaylistItems( + "snippet", + array("playlistId" => $likePlaylist) + ); +} + +echo pageHeader("User Query - Multiple APIs"); +if (strpos($client_id, "googleusercontent") == false) { + echo missingClientSecretsWarning(); + exit; +} +?> +
+
+Connect Me!"; +} else { + echo "

Results Of Drive List:

"; + foreach ($dr_results as $item) { + echo $item->title, "
\n"; + } + + echo "

Results Of YouTube Likes:

"; + foreach ($yt_results as $item) { + echo $item['snippet']['title'], "
\n"; + } +} ?> +
+
+'; //Client ID +$service_account_name = ''; //Email Address +$key_file_location = ''; //key.p12 + +echo pageHeader("Service Account Access"); +if (strpos($client_id, "googleusercontent") == false + || !strlen($service_account_name) + || !strlen($key_file_location)) { + echo missingServiceAccountDetailsWarning(); + exit; +} + +$client = new Google_Client(); +$client->setApplicationName("Client_Library_Examples"); +$service = new Google_Service_Books($client); + +/************************************************ + If we have an access token, we can carry on. + Otherwise, we'll get one with the help of an + assertion credential. In other examples the list + of scopes was managed by the Client, but here + we have to list them manually. We also supply + the service account + ************************************************/ +if (isset($_SESSION['service_token'])) { + $client->setAccessToken($_SESSION['service_token']); +} +$key = file_get_contents($key_file_location); +$cred = new Google_Auth_AssertionCredentials( + $service_account_name, + array('https://www.googleapis.com/auth/books'), + $key +); +$client->setAssertionCredentials($cred); +if ($client->getAuth()->isAccessTokenExpired()) { + $client->getAuth()->refreshTokenWithAssertion($cred); +} +$_SESSION['service_token'] = $client->getAccessToken(); + +/************************************************ + We're just going to make the same call as in the + simple query as an example. + ************************************************/ +$optParams = array('filter' => 'free-ebooks'); +$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); +echo "

Results Of Call:

"; +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "
\n"; +} + +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/simple-query.php b/includes/google-api-php-client-master/examples/simple-query.php new file mode 100644 index 0000000..c0f1149 --- /dev/null +++ b/includes/google-api-php-client-master/examples/simple-query.php @@ -0,0 +1,87 @@ +setApplicationName("Client_Library_Examples"); +$apiKey = ""; // Change this line. +// Warn if the API key isn't changed. +if (strpos($apiKey, "<") !== false) { + echo missingApiKeyWarning(); + exit; +} +$client->setDeveloperKey($apiKey); + +$service = new Google_Service_Books($client); + +/************************************************ + We make a call to our service, which will + normally map to the structure of the API. + In this case $service is Books API, the + resource is volumes, and the method is + listVolumes. We pass it a required parameters + (the query), and an array of named optional + parameters. + ************************************************/ +$optParams = array('filter' => 'free-ebooks'); +$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams); + +/************************************************ + This call returns a list of volumes, so we + can iterate over them as normal with any + array. + Some calls will return a single item which we + can immediately use. The individual responses + are typed as Google_Service_Books_Volume, but + can be treated as an array. + ***********************************************/ +echo "

Results Of Call:

"; +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "
\n"; +} + +/************************************************ + This is an example of deferring a call. + ***********************************************/ +$client->setDefer(true); +$optParams = array('filter' => 'free-ebooks'); +$request = $service->volumes->listVolumes('Henry David Thoreau', $optParams); +$results = $client->execute($request); + +echo "

Results Of Deferred Call:

"; +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "
\n"; +} + +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/simplefileupload.php b/includes/google-api-php-client-master/examples/simplefileupload.php new file mode 100644 index 0000000..4525998 --- /dev/null +++ b/includes/google-api-php-client-master/examples/simplefileupload.php @@ -0,0 +1,123 @@ +'; +$client_secret = ''; +$redirect_uri = ''; + +$client = new Google_Client(); +$client->setClientId($client_id); +$client->setClientSecret($client_secret); +$client->setRedirectUri($redirect_uri); +$client->addScope("https://www.googleapis.com/auth/drive"); +$service = new Google_Service_Drive($client); + +if (isset($_REQUEST['logout'])) { + unset($_SESSION['upload_token']); +} + +if (isset($_GET['code'])) { + $client->authenticate($_GET['code']); + $_SESSION['upload_token'] = $client->getAccessToken(); + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); +} + +if (isset($_SESSION['upload_token']) && $_SESSION['upload_token']) { + $client->setAccessToken($_SESSION['upload_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['upload_token']); + } +} else { + $authUrl = $client->createAuthUrl(); +} + +/************************************************ + If we're signed in then lets try to upload our + file. For larger files, see fileupload.php. + ************************************************/ +if ($client->getAccessToken()) { + // This is uploading a file directly, with no metadata associated. + $file = new Google_Service_Drive_DriveFile(); + $result = $service->files->insert( + $file, + array( + 'data' => file_get_contents(TESTFILE), + 'mimeType' => 'application/octet-stream', + 'uploadType' => 'media' + ) + ); + + // Now lets try and send the metadata as well using multipart! + $file = new Google_Service_Drive_DriveFile(); + $file->setTitle("Hello World!"); + $result2 = $service->files->insert( + $file, + array( + 'data' => file_get_contents(TESTFILE), + 'mimeType' => 'application/octet-stream', + 'uploadType' => 'multipart' + ) + ); +} + +echo pageHeader("File Upload - Uploading a small file"); +if (strpos($client_id, "googleusercontent") == false) { + echo missingClientSecretsWarning(); + exit; +} +?> +
+
+Connect Me!"; +} +?> +
+ +
+title); + var_dump($result2->title); +} +?> +
+
+ + + + " . $title . " + + + \n"; + if ($_SERVER['PHP_SELF'] != "/index.php") { + $ret .= "

Back

"; + } + $ret .= "

" . $title . "

"; + } + return $ret; +} + + +function pageFooter($file = null) +{ + $ret = ""; + if (isWebRequest()) { + // Echo the code if in an example. + if ($file) { + $ret .= "

Code:

"; + $ret .= "
";
+      $ret .= htmlspecialchars(file_get_contents($file));
+      $ret .= "
"; + } + $ret .= ""; + } + return $ret; +} + +function missingApiKeyWarning() +{ + $ret = ""; + if (isWebRequest()) { + $ret = " +

+ Warning: You need to set a Simple API Access key from the + Google API console +

"; + } else { + $ret = "Warning: You need to set a Simple API Access key from the Google API console:"; + $ret .= "\nhttp://developers.google.com/console\n"; + } + return $ret; +} + +function missingClientSecretsWarning() +{ + $ret = ""; + if (isWebRequest()) { + $ret = " +

+ Warning: You need to set Client ID, Client Secret and Redirect URI from the + Google API console +

"; + } else { + $ret = "Warning: You need to set Client ID, Client Secret and Redirect URI from the"; + $ret .= " Google API console:\nhttp://developers.google.com/console\n"; + } + return $ret; +} + +function missingServiceAccountDetailsWarning() +{ + $ret = ""; + if (isWebRequest()) { + $ret = " +

+ Warning: You need to set Client ID, Email address and the location of the Key from the + Google API console +

"; + } else { + $ret = "Warning: You need to set Client ID, Email address and the location of the Key from the"; + $ret .= " Google API console:\nhttp://developers.google.com/console\n"; + } + return $ret; +} diff --git a/includes/google-api-php-client-master/examples/user-example.php b/includes/google-api-php-client-master/examples/user-example.php new file mode 100644 index 0000000..df09e45 --- /dev/null +++ b/includes/google-api-php-client-master/examples/user-example.php @@ -0,0 +1,132 @@ +'; + $client_secret = ''; + $redirect_uri = ''; + +/************************************************ + Make an API request on behalf of a user. In + this case we need to have a valid OAuth 2.0 + token for the user, so we need to send them + through a login flow. To do this we need some + information from our API console project. + ************************************************/ +$client = new Google_Client(); +$client->setClientId($client_id); +$client->setClientSecret($client_secret); +$client->setRedirectUri($redirect_uri); +$client->addScope("https://www.googleapis.com/auth/urlshortener"); + +/************************************************ + When we create the service here, we pass the + client to it. The client then queries the service + for the required scopes, and uses that when + generating the authentication URL later. + ************************************************/ +$service = new Google_Service_Urlshortener($client); + +/************************************************ + If we're logging out we just need to clear our + local access token in this case + ************************************************/ +if (isset($_REQUEST['logout'])) { + unset($_SESSION['access_token']); +} + +/************************************************ + If we have a code back from the OAuth 2.0 flow, + we need to exchange that with the authenticate() + function. We store the resultant access token + bundle in the session, and redirect to ourself. + ************************************************/ +if (isset($_GET['code'])) { + $client->authenticate($_GET['code']); + $_SESSION['access_token'] = $client->getAccessToken(); + $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; + header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); +} + +/************************************************ + If we have an access token, we can make + requests, else we generate an authentication URL. + ************************************************/ +if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { + $client->setAccessToken($_SESSION['access_token']); +} else { + $authUrl = $client->createAuthUrl(); +} + +/************************************************ + If we're signed in and have a request to shorten + a URL, then we create a new URL object, set the + unshortened URL, and call the 'insert' method on + the 'url' resource. Note that we re-store the + access_token bundle, just in case anything + changed during the request - the main thing that + might happen here is the access token itself is + refreshed if the application has offline access. + ************************************************/ +if ($client->getAccessToken() && isset($_GET['url'])) { + $url = new Google_Service_Urlshortener_Url(); + $url->longUrl = $_GET['url']; + $short = $service->url->insert($url); + $_SESSION['access_token'] = $client->getAccessToken(); +} + +echo pageHeader("User Query - URL Shortener"); +if (strpos($client_id, "googleusercontent") == false) { + echo missingClientSecretsWarning(); + exit; +} +?> +
+
+Connect Me!"; +} else { + echo << + + + + Logout +END; +} +?> +
+ +
+ +
+
+