diff options
author | BlueRaja <BlueRaja.admin@gmail.com> | 2015-05-07 23:14:47 -0500 |
---|---|---|
committer | BlueRaja <BlueRaja.admin@gmail.com> | 2015-05-07 23:14:47 -0500 |
commit | 3dc3919ce1b5336861979cde56884842615c967b (patch) | |
tree | f0a2418290cecd15f20c834bb071ffa9f3694b09 /includes/google-api-php-client-master/examples | |
parent | 29e872fbc6c552ef02208fe9fa5416b69773aa38 (diff) | |
parent | c517b645c8723b5f4d20cbb91cbc4b9f45579cbb (diff) | |
download | pathery-3dc3919ce1b5336861979cde56884842615c967b.tar.xz |
Merge branch 'master' of git.raylu.net:pathery
Diffstat (limited to 'includes/google-api-php-client-master/examples')
12 files changed, 1137 insertions, 0 deletions
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 @@ +<?php +/* + * Copyright 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +session_start(); +include_once "templates/base.php"; + +/************************************************ + Make an API request authenticated via the + AppIdentity service on AppEngine. + ************************************************/ +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +echo pageHeader("AppIdentity Account Access"); + +$client = new Google_Client(); +$client->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 "<h3>Results Of Call:</h3>"; +echo "<pre>"; +var_dump($results); +echo "</pre>"; + +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 @@ +<?php +/* + * Copyright 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +echo pageHeader("Batching Queries"); + +/************************************************ + We're going to use the simple access to the + books API again as an example, but this time we + will batch up two queries into a single call. + ************************************************/ +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + We create the client and set the simple API + access key. If you comment out the call to + setDeveloperKey, the request may still succeed + using the anonymous quota. + ************************************************/ +$client = new Google_Client(); +$client->setApplicationName("Client_Library_Examples"); +$apiKey = "<YOUR_API_KEY>"; // 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 "<h3>Results Of Call 1:</h3>"; + foreach ($results['response-thoreau'] as $item) { + echo $item['volumeInfo']['title'], "<br /> \n"; + } + echo "<h3>Results Of Call 2:</h3>"; + foreach ($results['response-shaw'] as $item) { + echo $item['volumeInfo']['title'], "<br /> \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 @@ +<?php +/* + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +session_start(); + +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + We'll setup an empty 20MB file to upload. + ************************************************/ +DEFINE("TESTFILE", 'testfile.txt'); +if (!file_exists(TESTFILE)) { + $fh = fopen(TESTFILE, 'w'); + fseek($fh, 1024*1024*20); + fwrite($fh, "!", 1); + fclose($fh); +} + +/************************************************ + ATTENTION: Fill in these values! Make sure + the redirect URI is to this page, e.g: + http://localhost:8080/fileupload.php + ************************************************/ +$client_id = '<YOUR_CLIENT_ID>'; +$client_secret = '<YOUR_CLIENT_SECRET>'; +$redirect_uri = '<YOUR_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; +} +?> +<div class="box"> + <div class="request"> +<?php +if (isset($authUrl)) { + echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; +} +?> + </div> + + <div class="shortened"> +<?php +if (isset($result) && $result) { + var_dump($result); +} +?> + </div> +</div> +<?php +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/idtoken.php b/includes/google-api-php-client-master/examples/idtoken.php new file mode 100644 index 0000000..af21b8d --- /dev/null +++ b/includes/google-api-php-client-master/examples/idtoken.php @@ -0,0 +1,107 @@ +<?php +/* + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +session_start(); + +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + ATTENTION: Fill in these values! Make sure + the redirect URI is to this page, e.g: + http://localhost:8080/user-example.php + ************************************************/ +$client_id = '<YOUR_CLIENT_ID>'; +$client_secret = '<YOUR_CLIENT_SECRET>'; +$redirect_uri = '<YOUR_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; +} +?> +<div class="box"> + <div class="request"> +<?php +if (isset($authUrl)) { + echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; +} else { + echo "<a class='logout' href='?logout'>Logout</a>"; +} +?> + </div> + + <div class="data"> +<?php +if (isset($token_data)) { + var_dump($token_data); +} +?> + </div> +</div> +<?php +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/index.php b/includes/google-api-php-client-master/examples/index.php new file mode 100644 index 0000000..243c9c0 --- /dev/null +++ b/includes/google-api-php-client-master/examples/index.php @@ -0,0 +1,19 @@ +<?php +include_once "templates/base.php"; +if (!isWebRequest()) { + echo "To view this page on a webserver using PHP 5.4 or above run: \n\t + php -S localhost:8080\n"; + exit(); +} +echo pageHeader("PHP Library Examples"); ?> +<ul> + <li><a href="simple-query.php">A query using simple API access</a></li> + <li><a href="user-example.php">A query for user data, using OAuth 2.0 authentication.</a></li> + <li><a href="batch.php">An example of combining multiple calls into a batch request</a></li> + <li><a href="service-account.php">A query using the service account functionality.</a></li> + <li><a href="simplefileupload.php">An example of a small file upload.</a></li> + <li><a href="fileupload.php">An example of a large file upload.</a></li> + <li><a href="idtoken.php">An example of verifying and retrieving the id token.</a></li> + <li><a href="multi-api.php">An example of using multiple APIs.</a></li> +</ul> +<?php echo pageFooter(); diff --git a/includes/google-api-php-client-master/examples/multi-api.php b/includes/google-api-php-client-master/examples/multi-api.php new file mode 100644 index 0000000..e518b5d --- /dev/null +++ b/includes/google-api-php-client-master/examples/multi-api.php @@ -0,0 +1,114 @@ +<?php +/* + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +session_start(); + +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + ATTENTION: Fill in these values! Make sure + the redirect URI is to this page, e.g: + http://localhost:8080/user-example.php + ************************************************/ + $client_id = '<YOUR_CLIENT_ID>'; + $client_secret = '<YOUR_CLIENT_SECRET>'; + $redirect_uri = '<YOUR_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; +} +?> +<div class="box"> + <div class="request"> +<?php +if (isset($authUrl)) { + echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; +} else { + echo "<h3>Results Of Drive List:</h3>"; + foreach ($dr_results as $item) { + echo $item->title, "<br /> \n"; + } + + echo "<h3>Results Of YouTube Likes:</h3>"; + foreach ($yt_results as $item) { + echo $item['snippet']['title'], "<br /> \n"; + } +} ?> + </div> +</div> +<?php echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/service-account.php b/includes/google-api-php-client-master/examples/service-account.php new file mode 100644 index 0000000..46005f5 --- /dev/null +++ b/includes/google-api-php-client-master/examples/service-account.php @@ -0,0 +1,89 @@ +<?php +/* + * Copyright 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +session_start(); +include_once "templates/base.php"; + +/************************************************ + Make an API request authenticated with a service + account. + ************************************************/ +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + ATTENTION: Fill in these values! You can get + them by creating a new Service Account in the + API console. Be sure to store the key file + somewhere you can get to it - though in real + operations you'd want to make sure it wasn't + accessible from the webserver! + The name is the email address value provided + as part of the service account (not your + address!) + Make sure the Books API is enabled on this + account as well, or the call will fail. + ************************************************/ +$client_id = '<YOUR_CLIENT_ID>'; //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 "<h3>Results Of Call:</h3>"; +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "<br /> \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 @@ +<?php +/* + * Copyright 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +echo pageHeader("Simple API Access"); + +/************************************************ + Make a simple API request using a key. In this + example we're not making a request as a + specific user, but simply indicating that the + request comes from our application, and hence + should use our quota, which is higher than the + anonymous quota (which is limited per IP). + ************************************************/ +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + We create the client and set the simple API + access key. If you comment out the call to + setDeveloperKey, the request may still succeed + using the anonymous quota. + ************************************************/ +$client = new Google_Client(); +$client->setApplicationName("Client_Library_Examples"); +$apiKey = "<YOUR_API_KEY>"; // 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 "<h3>Results Of Call:</h3>"; +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "<br /> \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 "<h3>Results Of Deferred Call:</h3>"; +foreach ($results as $item) { + echo $item['volumeInfo']['title'], "<br /> \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 @@ +<?php +/* + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +session_start(); + +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + We'll setup an empty 1MB file to upload. + ************************************************/ +DEFINE("TESTFILE", 'testfile-small.txt'); +if (!file_exists(TESTFILE)) { + $fh = fopen(TESTFILE, 'w'); + fseek($fh, 1024 * 1024); + fwrite($fh, "!", 1); + fclose($fh); +} + +/************************************************ + ATTENTION: Fill in these values! Make sure + the redirect URI is to this page, e.g: + http://localhost:8080/fileupload.php + ************************************************/ +$client_id = '<YOUR_CLIENT_ID>'; +$client_secret = '<YOUR_CLIENT_SECRET>'; +$redirect_uri = '<YOUR_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; +} +?> +<div class="box"> + <div class="request"> +<?php +if (isset($authUrl)) { + echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; +} +?> + </div> + + <div class="shortened"> +<?php +if (isset($result) && $result) { + var_dump($result->title); + var_dump($result2->title); +} +?> + </div> +</div> +<?php +echo pageFooter(__FILE__); diff --git a/includes/google-api-php-client-master/examples/styles/style.css b/includes/google-api-php-client-master/examples/styles/style.css new file mode 100644 index 0000000..47dc4af --- /dev/null +++ b/includes/google-api-php-client-master/examples/styles/style.css @@ -0,0 +1,113 @@ +/* + * Copyright 2013 Google Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +body { + font-family: Arial,sans-serif; + margin: auto; + white-space: nowrap; + padding: 10px; +} + +.box { + border: .5em solid #E3E9FF; + -webkit-box-orient: vertical; + -webkit-box-align: center; + + -moz-box-orient: vertical; + -moz-box-align: center; + + display: block; + box-orient: vertical; + box-align: center; + + width: 400px; + height: auto; + + margin: auto; + padding: 10px; +} + +.request { + -webkit-box-flex: 1; + -moz-box-flex: 1; + box-flex: 1; +} + +.result { + -webkit-box-flex: 2; + -moz-box-flex: 2; + box-flex: 2; +} + +header { + color:#000; + padding:2px 5px; + font-size:100%; + margin: auto; + text-align: center +} + +header h1.logo { + margin:6px 0; + padding:0; + font-size:24px; + line-height:20px; + text-align: center +} + +.login { + font-size: 200%; + display: block; + margin: auto; + cursor: pointer; + text-align: center; + font-weight: bold; + color: #2779AA; + line-height: normal; +} + +.logout { + font-weight: normal; + margin-top: 0; +} + +.shortened { + overflow: scroll; +} + +.url { + color: #019; + font-size: 100%; + vertical-align: middle; + padding: 1px; + background-color: white; + border: 1px inset; + cursor: auto; + margin: 0; + text-indent: 0; + display: inline-block; +} + +pre.code { + padding: 10px; + border: .5em solid #E3E9FF; + margin: 10px; + height: 400px; + overflow: scroll; +} + +.warn { + color: red; +}
\ No newline at end of file diff --git a/includes/google-api-php-client-master/examples/templates/base.php b/includes/google-api-php-client-master/examples/templates/base.php new file mode 100644 index 0000000..34241e9 --- /dev/null +++ b/includes/google-api-php-client-master/examples/templates/base.php @@ -0,0 +1,90 @@ +<?php +/* Ad hoc functions to make the examples marginally prettier.*/ +function isWebRequest() +{ + return isset($_SERVER['HTTP_USER_AGENT']); +} + +function pageHeader($title) +{ + $ret = ""; + if (isWebRequest()) { + $ret .= "<!doctype html> + <html> + <head> + <title>" . $title . "</title> + <link href='styles/style.css' rel='stylesheet' type='text/css' /> + </head> + <body>\n"; + if ($_SERVER['PHP_SELF'] != "/index.php") { + $ret .= "<p><a href='index.php'>Back</a></p>"; + } + $ret .= "<header><h1>" . $title . "</h1></header>"; + } + return $ret; +} + + +function pageFooter($file = null) +{ + $ret = ""; + if (isWebRequest()) { + // Echo the code if in an example. + if ($file) { + $ret .= "<h3>Code:</h3>"; + $ret .= "<pre class='code'>"; + $ret .= htmlspecialchars(file_get_contents($file)); + $ret .= "</pre>"; + } + $ret .= "</html>"; + } + return $ret; +} + +function missingApiKeyWarning() +{ + $ret = ""; + if (isWebRequest()) { + $ret = " + <h3 class='warn'> + Warning: You need to set a Simple API Access key from the + <a href='http://developers.google.com/console'>Google API console</a> + </h3>"; + } 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 = " + <h3 class='warn'> + Warning: You need to set Client ID, Client Secret and Redirect URI from the + <a href='http://developers.google.com/console'>Google API console</a> + </h3>"; + } 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 = " + <h3 class='warn'> + Warning: You need to set Client ID, Email address and the location of the Key from the + <a href='http://developers.google.com/console'>Google API console</a> + </h3>"; + } 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 @@ +<?php +/* + * Copyright 2011 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +include_once "templates/base.php"; +session_start(); + +require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); + +/************************************************ + ATTENTION: Fill in these values! Make sure + the redirect URI is to this page, e.g: + http://localhost:8080/user-example.php + ************************************************/ + $client_id = '<YOUR_CLIENT_ID>'; + $client_secret = '<YOUR_CLIENT_SECRET>'; + $redirect_uri = '<YOUR_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; +} +?> +<div class="box"> + <div class="request"> +<?php +if (isset($authUrl)) { + echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; +} else { + echo <<<END + <form id="url" method="GET" action="{$_SERVER['PHP_SELF']}"> + <input name="url" class="url" type="text"> + <input type="submit" value="Shorten"> + </form> + <a class='logout' href='?logout'>Logout</a> +END; +} +?> + </div> + + <div class="shortened"> +<?php +if (isset($short)) { + var_dump($short); +} +?> + </div> +</div> +<?php +echo pageFooter(__FILE__); |