| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2011 Google Inc. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
include_once "templates/base.php"; |
| 18 |
session_start(); |
| 19 |
|
| 20 |
require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php'); |
| 21 |
|
| 22 |
/************************************************ |
| 23 |
ATTENTION: Fill in these values! Make sure |
| 24 |
the redirect URI is to this page, e.g: |
| 25 |
http://localhost:8080/user-example.php |
| 26 |
************************************************/ |
| 27 |
$client_id = '<YOUR_CLIENT_ID>'; |
| 28 |
$client_secret = '<YOUR_CLIENT_SECRET>'; |
| 29 |
$redirect_uri = '<YOUR_REDIRECT_URI>'; |
| 30 |
|
| 31 |
/************************************************ |
| 32 |
Make an API request on behalf of a user. In |
| 33 |
this case we need to have a valid OAuth 2.0 |
| 34 |
token for the user, so we need to send them |
| 35 |
through a login flow. To do this we need some |
| 36 |
information from our API console project. |
| 37 |
************************************************/ |
| 38 |
$client = new Google_Client(); |
| 39 |
$client->setClientId($client_id); |
| 40 |
$client->setClientSecret($client_secret); |
| 41 |
$client->setRedirectUri($redirect_uri); |
| 42 |
$client->addScope("https://www.googleapis.com/auth/drive"); |
| 43 |
$client->addScope("https://www.googleapis.com/auth/youtube"); |
| 44 |
|
| 45 |
/************************************************ |
| 46 |
We are going to create both YouTube and Drive |
| 47 |
services, and query both. |
| 48 |
************************************************/ |
| 49 |
$yt_service = new Google_Service_YouTube($client); |
| 50 |
$dr_service = new Google_Service_Drive($client); |
| 51 |
|
| 52 |
|
| 53 |
/************************************************ |
| 54 |
Boilerplate auth management - see |
| 55 |
user-example.php for details. |
| 56 |
************************************************/ |
| 57 |
if (isset($_REQUEST['logout'])) { |
| 58 |
unset($_SESSION['access_token']); |
| 59 |
} |
| 60 |
if (isset($_GET['code'])) { |
| 61 |
$client->authenticate($_GET['code']); |
| 62 |
$_SESSION['access_token'] = $client->getAccessToken(); |
| 63 |
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; |
| 64 |
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL)); |
| 65 |
} |
| 66 |
|
| 67 |
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { |
| 68 |
$client->setAccessToken($_SESSION['access_token']); |
| 69 |
} else { |
| 70 |
$authUrl = $client->createAuthUrl(); |
| 71 |
} |
| 72 |
|
| 73 |
/************************************************ |
| 74 |
If we're signed in, retrieve channels from YouTube |
| 75 |
and a list of files from Drive. |
| 76 |
************************************************/ |
| 77 |
if ($client->getAccessToken()) { |
| 78 |
$_SESSION['access_token'] = $client->getAccessToken(); |
| 79 |
|
| 80 |
$dr_results = $dr_service->files->listFiles(array('maxResults' => 10)); |
| 81 |
|
| 82 |
$yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true)); |
| 83 |
$likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes; |
| 84 |
$yt_results = $yt_service->playlistItems->listPlaylistItems( |
| 85 |
"snippet", |
| 86 |
array("playlistId" => $likePlaylist) |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
echo pageHeader("User Query - Multiple APIs"); |
| 91 |
if (strpos($client_id, "googleusercontent") == false) { |
| 92 |
echo missingClientSecretsWarning(); |
| 93 |
exit; |
| 94 |
} |
| 95 |
?> |
| 96 |
<div class="box"> |
| 97 |
<div class="request"> |
| 98 |
<?php |
| 99 |
if (isset($authUrl)) { |
| 100 |
echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>"; |
| 101 |
} else { |
| 102 |
echo "<h3>Results Of Drive List:</h3>"; |
| 103 |
foreach ($dr_results as $item) { |
| 104 |
echo $item->title, "<br /> \n"; |
| 105 |
} |
| 106 |
|
| 107 |
echo "<h3>Results Of YouTube Likes:</h3>"; |
| 108 |
foreach ($yt_results as $item) { |
| 109 |
echo $item['snippet']['title'], "<br /> \n"; |
| 110 |
} |
| 111 |
} ?> |
| 112 |
</div> |
| 113 |
</div> |
| 114 |
<?php echo pageFooter(__FILE__); |
| 115 |
|