multi-api.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
  20. /************************************************
  21. ATTENTION: Fill in these values! Make sure
  22. the redirect URI is to this page, e.g:
  23. http://localhost:8080/user-example.php
  24. ************************************************/
  25. $client_id = '<YOUR_CLIENT_ID>';
  26. $client_secret = '<YOUR_CLIENT_SECRET>';
  27. $redirect_uri = '<YOUR_REDIRECT_URI>';
  28. /************************************************
  29. Make an API request on behalf of a user. In
  30. this case we need to have a valid OAuth 2.0
  31. token for the user, so we need to send them
  32. through a login flow. To do this we need some
  33. information from our API console project.
  34. ************************************************/
  35. $client = new Google_Client();
  36. $client->setClientId($client_id);
  37. $client->setClientSecret($client_secret);
  38. $client->setRedirectUri($redirect_uri);
  39. $client->addScope("https://www.googleapis.com/auth/drive");
  40. $client->addScope("https://www.googleapis.com/auth/youtube");
  41. /************************************************
  42. We are going to create both YouTube and Drive
  43. services, and query both.
  44. ************************************************/
  45. $yt_service = new Google_Service_YouTube($client);
  46. $dr_service = new Google_Service_Drive($client);
  47. /************************************************
  48. Boilerplate auth management - see
  49. user-example.php for details.
  50. ************************************************/
  51. if (isset($_REQUEST['logout'])) {
  52. unset($_SESSION['access_token']);
  53. }
  54. if (isset($_GET['code'])) {
  55. $client->authenticate($_GET['code']);
  56. $_SESSION['access_token'] = $client->getAccessToken();
  57. $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  58. header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  59. }
  60. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  61. $client->setAccessToken($_SESSION['access_token']);
  62. } else {
  63. $authUrl = $client->createAuthUrl();
  64. }
  65. /************************************************
  66. If we're signed in, retrieve channels from YouTube
  67. and a list of files from Drive.
  68. ************************************************/
  69. if ($client->getAccessToken()) {
  70. $_SESSION['access_token'] = $client->getAccessToken();
  71. $dr_results = $dr_service->files->listFiles(array('maxResults' => 10));
  72. $yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true));
  73. $likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes;
  74. $yt_results = $yt_service->playlistItems->listPlaylistItems(
  75. "snippet",
  76. array("playlistId" => $likePlaylist)
  77. );
  78. }
  79. echo pageHeader("User Query - Multiple APIs");
  80. if (strpos($client_id, "googleusercontent") == false) {
  81. echo missingClientSecretsWarning();
  82. exit;
  83. }
  84. ?>
  85. <div class="box">
  86. <div class="request">
  87. <?php
  88. if (isset($authUrl)) {
  89. echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
  90. } else {
  91. echo "<h3>Results Of Drive List:</h3>";
  92. foreach ($dr_results as $item) {
  93. echo $item->title, "<br /> \n";
  94. }
  95. echo "<h3>Results Of YouTube Likes:</h3>";
  96. foreach ($yt_results as $item) {
  97. echo $item['snippet']['title'], "<br /> \n";
  98. }
  99. } ?>
  100. </div>
  101. </div>
  102. <?php echo pageFooter(__FILE__);