Brian Luong
Brian Luong

Reputation: 588

Adding permissions on Google Sheet doesn't work despite 200 from API

I'm using a service account with a project that has both GoogleDrive and Google Sheets API enabled. I am trying to add permissions to an already existing Google Sheet (created by the same service account). The API is returning like it succeeds but the permissions remained unchanged.

Client config:

from googleapiclient.discovery import build
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
GOOGLE_CLIENT = build('sheets', 'v4', credentials=creds)
GOOGLE_DRIVE_CLIENT = build('drive', 'v3', credentials=creds)

using Google Drive V3 API:

new_permission = {
    'type': "user",
    'emailAddress': '[email protected]',
    'role': "writer"
}

response = GOOGLE_DRIVE_CLIENT.permissions().create(
    fileId=GOOGLE_SHEETS_ID, fields='*', body=new_permission).execute()

The response I get is:

{'kind': 'drive#permission', 'id': '1X2680991XXX793XX1308', 'type': 'user', 'emailAddress': '[email protected]', 'role': 'writer', 'displayName': 'Jeff Bezos', 'deleted': False}

Then I run list permissions but it returns empty:

permissions = GOOGLE_DRIVE_CLIENT.permissions().list(fileId=GOOGLE_SHEETS_ID).execute()
print(permissions.get('items', []))

Upvotes: 0

Views: 133

Answers (1)

Tanaike
Tanaike

Reputation: 201378

  • You want to retrieve the permission list with the service account using Drive API v3.
  • You want to achieve this using google-api-python-client with python.
  • You have already been able to use Drive API.

If my understanding is correct, how about this answer? In your script, Drive API v3 is used. But from permissions.get('items', []), it seems that you are trying to use Drive API v2. By this, the empty array is returned. So how about the following modification?

From:

permissions = GOOGLE_DRIVE_CLIENT.permissions().list(fileId=GOOGLE_SHEETS_ID).execute()
print(permissions.get('items', []))

To:

permissions = service.permissions().list(fileId=fileId, fields='*').execute()
print(permissions.get('permissions', []))
  • In this modification, the permission list is retrieved with Drive API v3.

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

Upvotes: 3

Related Questions