简体   繁体   English

通过 label 查询 Google 云端硬盘文件失败

[英]Querying Google Drive files by label is failing

I need some help as I'm smashing my head on a wall.当我的头撞在墙上时,我需要一些帮助。

I need to write a script to run periodically on lambda that will pull values from some sheets in google drive.我需要编写一个脚本以在 lambda 上定期运行,它将从谷歌驱动器中的某些工作表中提取值。 The most straightforward way of finding these is to use the gdrive labels feature.找到这些最直接的方法是使用 gdrive 标签功能。 We've enabled it, created the label, and tagged some files.我们启用了它,创建了 label,并标记了一些文件。

I can then use the api explorer to query for all files with that label using this query 'labels/LYBX-my-label-id-bFcb' in labels然后,我可以使用 api 资源管理器'labels/LYBX-my-label-id-bFcb' in labels来查询具有该 label 的所有文件

I can also grab what my browser sent out and run it locally in postman or node/whatever.我还可以获取我的浏览器发送的内容,并在 postman 或节点/任何地方本地运行它。 It works and returns the expected file listings.它工作并返回预期的文件列表。

However that is using my personal account credentials and when doing this "for real" we need to use a service account of course.但是,这是使用我的个人帐户凭据,当“真正”执行此操作时,我们当然需要使用服务帐户。 So we created a GCP project with a service account, and I'm using the googleapiclient python package. I store the secret for that service account in aws secretmanager, fetch it, and configure my instance of the drive resource with it.因此,我们创建了一个带有服务帐户的 GCP 项目,我正在使用googleapiclient python package。我将该服务帐户的密码存储在 aws secretmanager 中,获取它,并使用它配置我的drive资源实例。

This all works.这一切都有效。 I can use it to call drive.files().get(...) and drive.files().list(...) and fetch data on files using all sorts of queries except the one I use above for the label. When I do that query I get back a 400 error that complains about the q (query) parameter.我可以使用它来调用drive.files().get(...)drive.files().list(...)并使用各种查询获取文件数据,但我在上面用于 label 的查询除外. 当我执行该查询时,我得到一个 400 错误,它抱怨q (查询)参数。

Now I've dropped down to the level of the url itself, and the exact GET request url that my python script logs works when I use my personal bearer token.现在我已经下降到 url 本身的级别,当我使用我的个人不记名令牌时,我的 python 脚本日志的确切GET 请求 url 有效。 I'm pretty sure therefore that this is not in fact a bad parameter issue and that's instead just a case of google being godawful at api design and returning crappy error codes.因此,我很确定这实际上不是一个错误的参数问题,而只是谷歌在 api 设计中表现糟糕并返回糟糕的错误代码的情况。

So I'm thinking that this has to be a permission issue, but I have no clue what permissions are required to allow an account to search by gdrive labels nor how I would go about granting those permissions to a service account.所以我认为这一定一个权限问题,但我不知道允许帐户按 gdrive 标签搜索需要什么权限,也不知道我将如何 go 将这些权限授予服务帐户。

Another possible clue is that drive.files().listLabels(fileId="...") on a file that I know has labels seems to fail, so again all points to some sort of permission being missing but its unclear which nor how to set those up on service accounts.另一个可能的线索是我知道有标签的文件上的drive.files().listLabels(fileId="...")似乎失败了,所以再次所有指向某种权限丢失但不清楚是哪个或如何在服务帐户上设置这些。

SUGGESTION建议

Note : Since I do not have visibility of your actual script, you can consider this answer as a starting point or reference for fixing the issue in your project.注意:由于我看不到您的实际脚本,您可以将此答案视为解决项目中问题的起点或参考。 Hopefully, this will resolve your problem.希望这能解决您的问题。

I conducted my own replication and successfully listed files by using a query based on the label ID with a service account through the process of user impersonation .我通过用户模拟过程使用基于 label ID 和服务帐户的查询进行了自己的复制并成功列出了文件。 This should be added in the credential creation phase, where you include a subject parameter to enable the service account to impersonate a user (such as a super admin account or any domain account with the necessary role) for service account delegation.这应该在凭证创建阶段添加,其中包含一个subject参数以使服务帐户能够模拟用户(例如超级管理员帐户或具有必要角色的任何域帐户)以进行服务帐户委托。

Test Script测试脚本

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

# Path to the service account JSON key file
KEY_FILE = 'sa.json'

# Create credentials from the service account key file & Build the service object
credentials = service_account.Credentials.from_service_account_file(
    KEY_FILE, scopes=['https://www.googleapis.com/auth/drive',
                      'https://www.googleapis.com/auth/drive.file',
                      'https://www.googleapis.com/auth/drive.metadata',
                      'https://www.googleapis.com/auth/drive.metadata.readonly',
                      'https://www.googleapis.com/auth/drive.readonly'],
                      subject="irv@■■■■■■■■■■■■■■.■■■■");

service = build('drive', 'v3', credentials=credentials);

# List files under a label
label_id = "OTVglmjg5BxgxSevMiuLtr6VoaeDwyg66AIRNNEbbFcb";
results = service.files().list(q= f"'labels/{label_id}' in labels").execute()

results

Demo演示

I have created a test label and tagged it with two files in my drive:我创建了一个测试 label 并在我的驱动器中用两个文件标记了它:

在此处输入图像描述

After running the test script:运行测试脚本后:

在此处输入图像描述

Reference参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将文件上传到 Google Drive - Unable to upload files to Google Drive Google Drive API 不显示共享文件 - Google Drive API doesn't show shared files 如何通过谷歌驱动器 api 获取/更改文件敏感度级别(标签)? - How get/change a File Sensitivity level (label) via google drive api? 使用 Google Apps 脚本将多个 .CSV 文件从 Google Drive 导入 BigQuery - Use Google Apps Script to import multiple .CSV files from Google Drive into BigQuery 有没有办法查询 Google Secrets Manager? - Is there a way of querying Google Secrets Manager? 当应用程序脚本文件或容器文件从云端硬盘中删除时,Google 应用程序脚本 GCP 项目不会从控制台中删除 - Google apps script GCP projects are not deleted from console when apps script files or container files are trashed from Drive 是否可以将 Azure 连接到谷歌驱动器? - Is it possible to connect Azure to google drive? 有没有办法在 Google Colab 中清理 Google Drive 垃圾桶? - Is there a way to clean Google Drive trashcan in Google Colab? 通过 Python 上传文件到 Google Drive - Uploading file to Google Drive Via Python 谷歌驱动器是否可以防止 zip 炸弹? - Does google drive protect against zip bombs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM