service-account.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * Copyright 2013 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. session_start();
  18. include_once "templates/base.php";
  19. /************************************************
  20. Make an API request authenticated with a service
  21. account.
  22. ************************************************/
  23. require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
  24. /************************************************
  25. ATTENTION: Fill in these values! You can get
  26. them by creating a new Service Account in the
  27. API console. Be sure to store the key file
  28. somewhere you can get to it - though in real
  29. operations you'd want to make sure it wasn't
  30. accessible from the webserver!
  31. The name is the email address value provided
  32. as part of the service account (not your
  33. address!)
  34. Make sure the Books API is enabled on this
  35. account as well, or the call will fail.
  36. ************************************************/
  37. $client_id = '<YOUR_CLIENT_ID>'; //Client ID
  38. $service_account_name = ''; //Email Address
  39. $key_file_location = ''; //key.p12
  40. echo pageHeader("Service Account Access");
  41. if (strpos($client_id, "googleusercontent") == false
  42. || !strlen($service_account_name)
  43. || !strlen($key_file_location)) {
  44. echo missingServiceAccountDetailsWarning();
  45. exit;
  46. }
  47. $client = new Google_Client();
  48. $client->setApplicationName("Client_Library_Examples");
  49. $service = new Google_Service_Books($client);
  50. /************************************************
  51. If we have an access token, we can carry on.
  52. Otherwise, we'll get one with the help of an
  53. assertion credential. In other examples the list
  54. of scopes was managed by the Client, but here
  55. we have to list them manually. We also supply
  56. the service account
  57. ************************************************/
  58. if (isset($_SESSION['service_token'])) {
  59. $client->setAccessToken($_SESSION['service_token']);
  60. }
  61. $key = file_get_contents($key_file_location);
  62. $cred = new Google_Auth_AssertionCredentials(
  63. $service_account_name,
  64. array('https://www.googleapis.com/auth/books'),
  65. $key
  66. );
  67. $client->setAssertionCredentials($cred);
  68. if ($client->getAuth()->isAccessTokenExpired()) {
  69. $client->getAuth()->refreshTokenWithAssertion($cred);
  70. }
  71. $_SESSION['service_token'] = $client->getAccessToken();
  72. /************************************************
  73. We're just going to make the same call as in the
  74. simple query as an example.
  75. ************************************************/
  76. $optParams = array('filter' => 'free-ebooks');
  77. $results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
  78. echo "<h3>Results Of Call:</h3>";
  79. foreach ($results as $item) {
  80. echo $item['volumeInfo']['title'], "<br /> \n";
  81. }
  82. echo pageFooter(__FILE__);