SERG
SERG

Reputation: 3971

How to create a spreadsheet with google api and set a proper permissions with PHP?

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!

Upvotes: 9

Views: 2887

Answers (3)

Hossein Shahsahebi
Hossein Shahsahebi

Reputation: 7288

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 protected]'
  ]
]);

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

{
 "kind": "drive#permission",
 "id": "14...",
 "type": "user",
 "role": "owner"
}

Upvotes: 1

Atul Jindal
Atul Jindal

Reputation: 976

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

Upvotes: 1

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

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.

Upvotes: 0

Related Questions