简体   繁体   English

无法通过驱动器 API 从服务帐户访问“共享”文件?

[英]Cannot access files "shared" from service account via drive API?

I have a folder on google drive which was created via a service account, and shared with my user.我在谷歌驱动器上有一个文件夹,它是通过服务帐户创建的,并与我的用户共享。

  • When acting as my user, I can see the folder without issue in the UI.作为我的用户时,我可以在 UI 中毫无问题地看到该文件夹。
  • When acting as the service account , I can list all of the files in the folder via API.作为服务帐号时,我可以通过 API 列出文件夹中的所有文件。

My user has been granted "editor" permission of the folder.我的用户已被授予该文件夹的“编辑”权限。

What I need to do is access the list of files in the folder via the API with the service account impersionating my user.我需要做的是通过 API 使用模拟我的用户的服务帐户访问文件夹中的文件列表。

I've setup domain wide delegation and included the scopes:我已经设置了域范围的委托并包括范围:

https://www.googleapis.com/auth/drive.file

https://www.googleapis.com/auth/drive

This query returns 100 results when acting as the service account , however when impersonating my user it returns 0 results.此查询在充当服务帐户时返回 100 个结果,但是在模拟我的用户时返回0 个结果。

const auth = new google.auth.GoogleAuth({
    clientOptions: {
        subject: 'myemail@myorganization.com'
    },
    keyFile: './token.json',
    scopes: ['https://www.googleapis.com/auth/drive.file'],
});

drive.files.list({
    q: "'THE_FOLDER_ID' in parents",
    fields: 'nextPageToken, files(id, name, parents,  appProperties)',
    includeItemsFromAllDrives: true,
    supportsAllDrives: true,
    fields: '*',
    spaces: 'drive'
}).then(response => {
    console.log(response.data.files.length)
})

Turns out I needed to use the scope "https://www.googleapis.com/auth/drive" instead of "https://www.googleapis.com/auth/drive.file"结果我需要使用 scope“https://www.googleapis.com/auth/drive”而不是“https://www.googleapis.com/auth/drive.file”

暂无
暂无

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

相关问题 Google Drive API 不显示共享文件 - Google Drive API doesn't show shared files 如何在不下载服务帐户凭据的情况下从 Google Compute Engine 和本地验证 Google API(Google Drive API)? - How to authenticate Google APIs (Google Drive API) from Google Compute Engine and locally without downloading Service Account credentials? 如何使用 REST API 从服务帐户凭据创建访问令牌? - How do I create an Access Token from Service Account Credentials using REST API? 如何检查 GCP 服务帐户是否具有对 Google Drive 的完全访问权限? - How do I check to see if a GCP service account has full access to Google Drive? 限制服务内的服务帐户访问 - Restricting service account access within a service 是否可以从已被授权访问 GS 存储桶的谷歌帐户授权谷歌服务帐户? - is it possible to authorize google service account from google account that is already authorized to have access on GS buckets? “没有OAuth的服务帐户授权”,我们可以通过这种方法从谷歌云存储中获取文件吗? - "Service account authorization without OAuth", can we get file from Google Cloud Storage via this approach? 来自 Firebase 和来自 GCloud 的 Firebase 服务帐户密钥文件之间的区别? - Difference between Firebase service account key files from Firebase and from GCloud? 如何使用服务帐户从 python 代码访问 GCP 云 function? - How I can get access to GCP cloud function from python code using service account? 为 gmail API 创建服务帐户凭据 - Create service account credentials for gmail API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM