Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3.  
  4. define('APPLICATION_NAME', 'Google Sheets API PHP Quickstart');
  5. define('CREDENTIALS_PATH', '~/.credentials/sheets.googleapis.com-php-quickstart.json');
  6. define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
  7. // If modifying these scopes, delete your previously saved credentials
  8. // at ~/.credentials/sheets.googleapis.com-php-quickstart.json
  9. define('SCOPES', implode(' ', array(
  10. Google_Service_Sheets::SPREADSHEETS_READONLY)
  11. ));
  12.  
  13. if (php_sapi_name() != 'cli') {
  14. throw new Exception('This application must be run on the command line.');
  15. }
  16.  
  17. /**
  18. * Returns an authorized API client.
  19. * @return Google_Client the authorized client object
  20. */
  21. function getClient() {
  22. $client = new Google_Client();
  23. $client->setApplicationName(APPLICATION_NAME);
  24. $client->setScopes(SCOPES);
  25. $client->setAuthConfig(CLIENT_SECRET_PATH);
  26. $client->setAccessType('offline');
  27.  
  28. // Load previously authorized credentials from a file.
  29. $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
  30. if (file_exists($credentialsPath)) {
  31. $accessToken = json_decode(file_get_contents($credentialsPath), true);
  32. } else {
  33. // Request authorization from the user.
  34. $authUrl = $client->createAuthUrl();
  35. printf("Open the following link in your browser:n%sn", $authUrl);
  36. print 'Enter verification code: ';
  37. $authCode = trim(fgets(STDIN));
  38.  
  39. // Exchange authorization code for an access token.
  40. $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
  41.  
  42. // Store the credentials to disk.
  43. if(!file_exists(dirname($credentialsPath))) {
  44. mkdir(dirname($credentialsPath), 0700, true);
  45. }
  46. file_put_contents($credentialsPath, json_encode($accessToken));
  47. printf("Credentials saved to %sn", $credentialsPath);
  48. }
  49. $client->setAccessToken($accessToken);
  50.  
  51. // Refresh the token if it's expired.
  52. if ($client->isAccessTokenExpired()) {
  53. $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
  54. file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
  55. }
  56. return $client;
  57. }
  58.  
  59. /**
  60. * Expands the home directory alias '~' to the full path.
  61. * @param string $path the path to expand.
  62. * @return string the expanded path.
  63. */
  64. function expandHomeDirectory($path) {
  65. $homeDirectory = getenv('HOME');
  66. if (empty($homeDirectory)) {
  67. $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH');
  68. }
  69. return str_replace('~', realpath($homeDirectory), $path);
  70. }
  71.  
  72. // Get the API client and construct the service object.
  73. $client = getClient();
  74. $service = new Google_Service_Sheets($client);
  75.  
  76. // Prints the names and majors of students in a sample spreadsheet:
  77.  
  78. $spreadsheetId = 'myfileid';
  79. $range = 'Class Data!A2';
  80. $response = $service->spreadsheets_values->get($spreadsheetId, $range);
  81. $values = $response->getValues();
  82.  
  83. if (count($values) == 0) {
  84. print "No data found.n";
  85. } else {
  86. print "Name, Major:n";
  87. foreach ($values as $row) {
  88. // Print columns A and E, which correspond to indices 0 and 4.
  89. printf("%s, %sn", $row[0], $row[4]);
  90. }
  91. }
  92.  
  93. $values = array(
  94. array(
  95. 5
  96. ),
  97. // Additional rows ...
  98. );
  99. $body = new Google_Service_Sheets_ValueRange(array(
  100. 'values' => $values
  101. ));
  102. $params = array(
  103. 'valueInputOption' => $valueInputOption
  104. );
  105. $result = $service->spreadsheets_values->update($spreadsheetId, $range,
  106. $body, $params);
  107.  
  108. Uncaught exception 'Google_Service_Exception' with message '{
  109. "error": {
  110. "code": 403,
  111. "message": "Request had insufficient authentication scopes.",
  112. "errors": [
  113. {
  114. "message": "Request had insufficient authentication scopes.",
  115. "domain": "global",
  116. "reason": "forbidden"
  117. }
  118. ],
  119. "status": "PERMISSION_DENIED"
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement