googleworkspace / php-samples

PHP samples for Google Workspace APIs
Apache License 2.0
288 stars 348 forks source link

basic writing giving HTTP ERROR 500 #28

Closed sp487h closed 5 years ago

sp487h commented 5 years ago

Hi,

basic reading is working fine but when I am trying to write some values in google sheet it giving http error 500

SAMPLE

 <?php

require __DIR__ . '/vendor/autoload.php';

function getClient()
{
   $client = new Google_Client();
   $client->setApplicationName('Google Sheets API PHP Quickstart');
   $client->setScopes(Google_Service_Sheets::SPREADSHEETS);
   $client->setAuthConfig('credentials.json');
   $client->setAccessType('offline');

  $credentialsPath = 'token.json';
  if (file_exists($credentialsPath)) {
       $accessToken = json_decode(file_get_contents($credentialsPath), true);
     } else {
      $authUrl = $client->createAuthUrl();
      printf("Open the following link in your browser:\n%s\n", $authUrl);
      print 'Enter verification code: ';
      $authCode = trim(fgets(STDIN));

      $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

       if (array_key_exists('error', $accessToken)) {
          throw new Exception(join(', ', $accessToken));
         }
        if (!file_exists(dirname($credentialsPath))) {
          mkdir(dirname($credentialsPath), 0700, true);
        }
       file_put_contents($credentialsPath, json_encode($accessToken));
       printf("Credentials saved to %s\n", $credentialsPath);
 }
     $client->setAccessToken($accessToken);

     if ($client->isAccessTokenExpired()) {
        $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
     }
return $client;

}

$client = getClient();

$service = new Google_Service_Sheets($client);

$spreadsheetId = '13zWB1_uY5CdPGLuSXRWAgD0lE7QVrbTDW_9V5lqdNAY';

$range = "Sheet1!A1:C3";

$options = array('valueInputOption' => 'RAW');

$values = [
   ["Name", "Roll No.", "Contact"],
   ["Anis", "001", "+88017300112233"],
   ["Ashik", "002", "+88017300445566"]
   ];

$body   = new Google_Service_Sheets_ValueRange(['values' => $values]);

  $result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $range, $body, $options);

    ?>

Expected Behavior : Sheet should updated with mentioned values

Actual Behavior : getting http 500 error

Specifications

erickoledadevrel commented 5 years ago

You should be using the update method, not the batchUpdate method, if you are only modifying a single range of values:

https://developers.google.com/sheets/api/guides/values#writing_to_a_single_range

sanupanji commented 5 years ago

can you give a sample php code