Created
October 16, 2019 22:32
-
-
Save dansayo/d1f8daa9828457ea894f0a30b0fb4f69 to your computer and use it in GitHub Desktop.
Setting up a subscriber to changes in a Google folder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python 3.7.4 | |
import json | |
import datetime # for the expiration property: a date string to epoch millisec | |
import uuid # for the id property | |
from apiclient import discovery | |
from google.oauth2 import service_account | |
try: | |
ep = lambda year, month, day, hour, min: int( | |
datetime.datetime(year, month, day, hour, min).timestamp() | |
) | |
API = "drive" | |
API_VERSION = "v3" # from "v3" | |
scopes = [ | |
"https://www.googleapis.com/auth/drive", | |
"https://www.googleapis.com/auth/drive.file", | |
] | |
with open("client_servacc.json", "r") as read_file: | |
serv_acct_info = json.load(read_file) | |
# this needs to change to get credentials from a string object instead of a file | |
# https://google-auth.readthedocs.io/en/latest/reference/google.oauth2.service_account.html | |
# in preparation for AWS SSM storage | |
credentials = service_account.Credentials.from_service_account_info( | |
serv_acct_info, scopes=scopes | |
) | |
drive_service = discovery.build(API, API_VERSION, credentials=credentials) | |
folderId = "<the Google folder id>" | |
# request body | |
data = { | |
"id": str(uuid.uuid4()), | |
"type": "web_hook", | |
"address": "<the URL to use to receive notifications, must be SSL certified", | |
"expiration": ep(2019, 10, 31, 6, 0), | |
} | |
response = drive_service.files().watch(fileId=folderId, body=data).execute() | |
print(response) | |
# sample successful request response | |
# https://developers.google.com/drive/api/v3/reference/files/watch?authuser=0#response_1 | |
except OSError as e: | |
print(e) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment