9

I have this

define('CLIENT_SECRET_PATH', __DIR__ . '/config_api.json');
define('ACCESS_TOKEN', '0b502651********c52b3');

I can create a spreadsheet with this and get the id and url.

$requestBody = new Google_Service_Sheets_Spreadsheet();
$response = $service->spreadsheets->create($requestBody);
print_r($response);
$new_spr_id = $response['spreadsheetId'];

But this spreadsheet does not appears in the google sheets list as it is "protected" or something. I am trying to set the permissions with this but get an error: Fatal error: Call to undefined method Google_Service_Drive_Permission::setValue()

insertPermission($service, $new_spr_id, '**@gmail.com' , 'user', 'owner');
function insertPermission($service, $fileId, $value, $type, $role) {
  $newPermission = new Google_Service_Drive_Permission();
  $newPermission->setValue($value);
  $newPermission->setType($type);
  $newPermission->setRole($role);
  try {
    return $service->permissions->insert($fileId, $newPermission);
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
  return NULL;
}

I need an example of creating a new spreadsheet and setting the proper permissions to it so I can modify this spreadsheet from my account etc.

Many many thanks!

SERG
  • 3,907
  • 8
  • 44
  • 89

3 Answers3

1

Your code is not able to locate class and unable to create instance of Google_Service_Drive_Permission(). I would suggest, don't use individual function to create object of Google_Service_Drive_Permission(). Place all your code to set permissions within the code part, where you are creating file. Also if you are using multiple files, check if your files are loading properly and are located by PHP parser. Because Fatal Error for undefined method call is not due to implementation of API methods, its due to calling for methods that does not exist or you PHP parser is unable to locate.

For reference this might be helpful

http://hotexamples.com/examples/-/Google_Service_Drive_Permission/setValue/php-google_service_drive_permission-setvalue-method-examples.html

Atul Jindal
  • 946
  • 8
  • 8
  • -1 because `Google_Service_Drive_Permission()` instance is already created and the fatal error is related to the absence of a method called `setValue` in that class. We can replace the `setValue` method with `setEmailAddress`. However, later another error will be triggered for `...->permissions->insert()` as `permissions` type is array. It is `Google API PHP Client` issue and I haven't found any workaround yet – Hossein Shahsahebi May 05 '20 at 13:19
1

I had the same problem and wasn't able to figure it out using Google API PHP Client methods designed specifically for altering permissions. However, there is a possibility to retrieve a Guzzle instance with the authentication info from PHP Client. Therefore, we can simply call the desired API endpoint to send the request. The code below is a complete workaround for changing file owner/permission on Google Drive:

//if you're using Service Account, otherwise follow your normal authorization
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/json');
$client = new Google_Client();
$client->setScopes(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();

//Now the main code to change the file permission begins
$httpClient = $client->authorize(); //It returns a Guzzle instance with proper Headers
$result = $httpClient->request('POST', 'https://www.googleapis.com/drive/v3/files/[FILE_ID]/permissions?transferOwnership=true', [
  'json' => [
    'role' => 'owner',
    'type' => 'user', 
    'emailAddress' => 'email@example.com'
  ]
]);

And the sample response of $result->getBody()->getContents() is:

{
 "kind": "drive#permission",
 "id": "14...",
 "type": "user",
 "role": "owner"
}
Hossein Shahsahebi
  • 6,348
  • 5
  • 24
  • 38
0

I think you're on the wrong API. Setting permission for files are found in Drive API permissions.

But to answer your question, here's how to create a new spreadsheet using spreadsheets.create from the Sheets API:

<?php
/*
 * BEFORE RUNNING:
 * ---------------
 * 1. If not already done, enable the Google Sheets API
 *    and check the quota for your project at
 *    https://console.developers.google.com/apis/api/sheets
 * 2. Install the PHP client library with Composer. Check installation
 *    instructions at https://github.com/google/google-api-php-client.
 */

// Autoload Composer.
require_once __DIR__ . '/vendor/autoload.php';

$client = getClient();

$service = new Google_Service_Sheets($client);

// TODO: Assign values to desired properties of `requestBody`:
$requestBody = new Google_Service_Sheets_Spreadsheet();

$response = $service->spreadsheets->create($requestBody);

// TODO: Change code below to process the `response` object:
echo '<pre>', var_export($response, true), '</pre>', "\n";

function getClient() {
  // TODO: Change placeholder below to generate authentication credentials. See
  // https://developers.google.com/sheets/quickstart/php#step_3_set_up_the_sample
  //
  // Authorize using one of the following scopes:
  //   'https://www.googleapis.com/auth/drive'
  //   'https://www.googleapis.com/auth/spreadsheets'
  return null;
}
?>

When the files has been created and saved in your Google Drive, you can now try to set Permissions using the Drive REST API.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • 1
    Thanks for the answer, but I am able to create a new spreadsheet with $requestBody = new Google_Service_Sheets_Spreadsheet(); $response = $service->spreadsheets->create($requestBody); I get the url and the id of the spreadsheet, but I can't access it from the account or api, so I am looking for an example about how to set the permissions with API. – SERG Apr 03 '17 at 06:06