【发布时间】:2022-08-17 03:39:45
【问题描述】:
我正在开发一个与 Google Drive 交互的应用程序,其工作方式如下:当用户在 Drive Share 中添加/修改文件时,我的应用程序将收到通知并由我处理。我使用 Auth2 身份验证在本地进行开发,一切正常,但此应用程序将托管在 Cloud Functions 上,因此我无法使用 Auth2 身份验证,因为需要用户同意。 由于这个问题,我进入了使用服务帐户的角度,将其添加为我的共享驱动器的管理器,使用它来创建功能,并赋予它所有必要的权限,但是当我修改文件时,我应该接收消息的端点,只是没有。 我进行了搜索,发现这是由于服务帐户无法访问用户数据,因此不会创建任何通知是有道理的。 下面我附上我用来在驱动器上创建观察程序的代码和 SA 的身份验证过程:
负责获取身份验证凭据的代码
SCOPES = [
\"https://www.googleapis.com/auth/drive\",
\"https://www.googleapis.com/auth/drive.file\",
\"https://www.googleapis.com/auth/drive.readonly\",
\"https://www.googleapis.com/auth/drive.metadata.readonly\"
]
credentials, project_id = google.auth.default(scopes=SCOPES)
credentials.refresh(req.Request())
负责创建手表的代码
drive = discovery.build(\"drive\", \"v3\", credentials=credentials)
params = {
\"kind\": \"api#channel\",
\"id\": \"id_watcher\",
\"type\": \"webhook\",
\"address\": \"address cloud functions\"
}
# r = drive.changes().watch(fileId=file_id, body=params, supportsAllDrives=True, supportsTeamDrives=True).execute()
r = drive.changes().watch(pageToken=1,
body=params,
driveId=driverId,
includeCorpusRemovals=True,
includeItemsFromAllDrives=True,
includePermissionsForView=None,
includeRemoved=True,
includeTeamDriveItems=True,
pageSize=None,
restrictToMyDrive=None,
spaces=None,
supportsAllDrives=True,
# supportsTeamDrives=True,
# teamDriveId=driverId
).execute()
我的问题是,是否有一种无需用户同意即可使用 Auth2 的方法,即无需打开浏览器并允许生成令牌的步骤。如果没有,你能帮我一个可行的方法吗?
请记住,此代码将在云函数中。
非常感谢!
标签: google-cloud-functions google-drive-api google-oauth service-accounts file-watcher