user-example.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/urlshortener");
  40. /************************************************
  41. When we create the service here, we pass the
  42. client to it. The client then queries the service
  43. for the required scopes, and uses that when
  44. generating the authentication URL later.
  45. ************************************************/
  46. $service = new Google_Service_Urlshortener($client);
  47. /************************************************
  48. If we're logging out we just need to clear our
  49. local access token in this case
  50. ************************************************/
  51. if (isset($_REQUEST['logout'])) {
  52. unset($_SESSION['access_token']);
  53. }
  54. /************************************************
  55. If we have a code back from the OAuth 2.0 flow,
  56. we need to exchange that with the authenticate()
  57. function. We store the resultant access token
  58. bundle in the session, and redirect to ourself.
  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 we have an access token, we can make
  68. requests, else we generate an authentication URL.
  69. ************************************************/
  70. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  71. $client->setAccessToken($_SESSION['access_token']);
  72. } else {
  73. $authUrl = $client->createAuthUrl();
  74. }
  75. /************************************************
  76. If we're signed in and have a request to shorten
  77. a URL, then we create a new URL object, set the
  78. unshortened URL, and call the 'insert' method on
  79. the 'url' resource. Note that we re-store the
  80. access_token bundle, just in case anything
  81. changed during the request - the main thing that
  82. might happen here is the access token itself is
  83. refreshed if the application has offline access.
  84. ************************************************/
  85. if ($client->getAccessToken() && isset($_GET['url'])) {
  86. $url = new Google_Service_Urlshortener_Url();
  87. $url->longUrl = $_GET['url'];
  88. $short = $service->url->insert($url);
  89. $_SESSION['access_token'] = $client->getAccessToken();
  90. }
  91. echo pageHeader("User Query - URL Shortener");
  92. if (strpos($client_id, "googleusercontent") == false) {
  93. echo missingClientSecretsWarning();
  94. exit;
  95. }
  96. ?>
  97. <div class="box">
  98. <div class="request">
  99. <?php
  100. if (isset($authUrl)) {
  101. echo "<a class='login' href='" . $authUrl . "'>Connect Me!</a>";
  102. } else {
  103. echo <<<END
  104. <form id="url" method="GET" action="{$_SERVER['PHP_SELF']}">
  105. <input name="url" class="url" type="text">
  106. <input type="submit" value="Shorten">
  107. </form>
  108. <a class='logout' href='?logout'>Logout</a>
  109. END;
  110. }
  111. ?>
  112. </div>
  113. <div class="shortened">
  114. <?php
  115. if (isset($short)) {
  116. var_dump($short);
  117. }
  118. ?>
  119. </div>
  120. </div>
  121. <?php
  122. echo pageFooter(__FILE__);