【问题标题】:How to authenticate a cloud functions with the Api https://www.googleapis.com/drive/v3/changes/watch?如何使用 Api https://www.googleapis.com/drive/v3/changes/watch 对云功能进行身份验证?
【发布时间】: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


    【解决方案1】:

    两种建议,一种用于用户同意方案,另一种用于通知:

    域范围的委托和模拟

    如果您正在使用 G Suite 并为组织构建应用程序。如果您使用服务帐户,则可以使用Domain Wide Delegation。这将允许您开始模拟过程并避免用户的同意。

    正如官方documentation 所建议的那样,您还可以申请并查看授予服务帐户模拟用户选项的方法。

    生成通知

    另一个建议可能是使用 Pub/Sub 或推送通知来获取警报。您将能够使用服务帐户和您的代码并获取通知并将它们类似于审核日志:

    该图像是用于监视和列表的 Gmail APi 示例。

    参考:

    【讨论】:

      猜你喜欢
      • 2019-12-02
      • 2017-08-16
      • 1970-01-01
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      相关资源
      最近更新 更多