观看电子表格中使用Google服务帐户进行的更改吗?

时间:2019-07-15 18:09:20

标签: google-api google-drive-api google-oauth

我有一个服务帐户,可用于访问电子表格中的数据:

credentials = service_account.Credentials.from_service_account_file(
            SERVICE_ACCOUNT_KEY, scopes=SCOPES)        
        service = discovery.build('sheets', 'v4', credentials=credentials)
result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheet_id, range=range_name).execute()

我希望能够观看电子表格并在发生更改时收到警报。 The Google documentation建议我需要使用直接HTTP请求来做到这一点:

POST https://www.googleapis.com/drive/v3/files/fileId/watch
Authorization: Bearer auth_token_for_current_user
Content-Type: application/json

{
  "id": "01234567-89ab-cdef-0123456789ab", // Your channel ID.
  "type": "web_hook",
  "address": "https://example.com/notifications", // Your receiving URL.
  ...
  "token": "target=myApp-myFilesChannelDest", // (Optional) Your channel token.
  "expiration": 1426325213000 // (Optional) Your requested channel expiration time.
}

如何获取auth令牌来执行此操作?

1 个答案:

答案 0 :(得分:0)

您不需要使用HTTP请求, 您要做的就是打开目标Google电子表格, 工具/实用程序-> 脚本编辑器

在Google Apps脚本中,您可以为电子表格中的更改设置触发器。 click that lil timer icon right below the menu

此外,请参考此文档编写一个.gs文件,以决定发生更改(触发脚本)时该怎么做

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app