Google sheets API v4 Create sheet and invite users

Mike

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 = '[email protected]';
    $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 = '[email protected]

    $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"?

DaImTo

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How To Use Google Sheets API v4 To Create New Sheet or Tab in Spreadsheet with PHP

Create spreadsheet with Google Sheets API V4 API Key

How to remove a blank row in google sheet using google sheets api v4 javascript?

Get list of sheets and latest sheet in google spreadsheet api v4 in Python

Google Sheets v4 API: unusual 503 when cloning sheet

Android Google Sheets API V4 - Update public sheet without OAuth

How to call a specific sheet within a spreadsheet via the Google Sheets API v4 in Python

Looking up Google Sheets values using sheet id instead of sheet title (Google Drive Rest API V4)

How can I get single sheet from a spreadsheet collection from Google Sheets with Google Sheets API v4?

Are service accounts considered as users in Google Sheets API v4 usage limits?

How to insert the current date time into a google sheet, respecting the sheet's timezone, using sheets api v4?

Google Sheets API v4 and valueInputOption

Google sheet api v4 dynamic sheet range

Find cell by value with google api v4 (google sheets)

Google Sheets API V4 and google apps script

Write to Sheet with Google Sheets API

Google Sheet API V4(Java) append Date in cells

How to append rows with google sheet api v4 in nodejs

Why are some Google Sheets API v4 functions not documented?

Reading whole Google Spreadsheet with Sheets API v4 Java

Google Sheets API v4 write data

How to format a cell with Google Sheets API v4 and Go?

How to update google spreadsheet via sheets API v4

Google Sheets API v4 - bold part of cell

How to Get Hyperlinks Using Google Sheets API v4

Golang google sheets API V4 - Write/Update example?

Google Sheets API v4 dot Net Deleting a row

Google Sheets API v4: Add named or protected ranges

Google Sheets API v4: batchGet not working