10

I am using a service account authentication to create a google sheet using the Google Sheets API. I want to create a spreadsheet and somehow allow my team to open it.

    $private_key  = file_get_contents($rootDir . '/config/googleApiCredentials.p12');
    $client_email = 'project-names@positive-water-134xxx.iam.gserviceaccount.com';
    $scopes       = implode(' ', ["https://www.googleapis.com/auth/drive"]);

    $credentials = new \Google_Auth_AssertionCredentials(
        $client_email,
        $scopes,
        $private_key
    );
    // tried once with additional constructor params:
    // $privateKeyPassword = 'notasecret',
    // $assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
    // $sub = 'myMail@gmail.com

    $this->googleClient = new \Google_Client();
    $this->googleClient->setAssertionCredentials($credentials);
    if ($this->googleClient->getAuth()->isAccessTokenExpired()) {
        $this->googleClient->getAuth()->refreshTokenWithAssertion();
    }
    $service = new \Google_Service_Sheets($this->googleClient);
    $newSheet = new \Google_Service_Sheets_Spreadsheet();
    $newSheet->setSpreadsheetId('34534534'); // <- hardcoded for test
    $response = $service->spreadsheets->create($newSheet);
    print_r($response);

The sheet is beeing created, I receive response with

[spreadsheetId] => 34534534

However, I can't open this file through browser, using my google's account, even when Im added as the owner, editor, writer and reader in Google's developer console, in project's permissions. Browser says that I'm unathorized and I can contact administrator for permissions

Any suggestions? Has anyone managed how to create documents and give access to it for any "human"?

Mike
  • 447
  • 4
  • 15
  • who are you authenticating as when you create the file? what do you mean by goole-api-services ? Also please add the create for $service so we can see how you are authenticating it might be easer. – Linda Lawton - DaImTo Jun 22 '16 at 12:29
  • @DalmTo, I'm using Service account created in developers console. It looks like: project-name@positive-water-134xxx.iam.gserviceaccount.com – Mike Jun 22 '16 at 12:31

2 Answers2

6

A service account is a dummy user. It has its own drive account it owns the file it created on its account. The service account will need to give your personal google drive account access to said file. (Share it with you just like any other user)

Permissions: insert Inserts a permission for a file.

Note: You are currently authenticated to use the Google sheets API you are going to have to add google drive API (scope) to your code and your project on Google developers console. There is no way to change the permissions on a file via the google sheets API.

Yes you are going to have to add permissions for every member of your team who you want to allow to see / edit this file.

Tip: When you add google drive API try and do a files.list look for downloadUrl, alternateLink and embedLink in the results. Sometimes these are filled out and can be used for sharing viewable only to another user.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • Thanks, will try! Do youknow if there is any way to add permission for multiple users at once using aliases? – Mike Jun 22 '16 at 12:42
  • nope you have to add them one at a time. However check out the new v3 of the google drive api first my links are all v2 its what I have the most experience with. It might have some better options. – Linda Lawton - DaImTo Jun 22 '16 at 12:45
5

It is possible to give permissions for domain accounts:

private function createNewSheet($title)
{
    $properties = new \Google_Service_Sheets_SpreadsheetProperties();
    $properties->setTitle($title);

    $newSheet = new \Google_Service_Sheets_Spreadsheet();
    $newSheet->setProperties($properties);

    $response = $this->sheetService->spreadsheets->create($newSheet);
    $fileId = $response['spreadsheetId'];

    $domainPermission = new \Google_Service_Drive_Permission([
        'type'  => 'domain',
        'role'  => 'writer',
        'value' => 'myCompany.com' //eg user@myCompany.com
    ]);

    $this->driveService->permissions->insert($fileId, $domainPermission);
    return $response;
}
Mike
  • 447
  • 4
  • 15