简体   繁体   English

谷歌服务帐户找不到.xlsx 文件

[英]google service account not finding .xlsx files

I am trying to use a google drive folder to store data then read and manipulate it with python using gspread.我正在尝试使用谷歌驱动器文件夹来存储数据,然后使用 gspread 使用 python 读取和操作它。 The steps I followed in order to try and do this are:为了尝试执行此操作,我遵循的步骤是:

  1. create a folder with my spread sheets (.xlsx files) in a subfolder within this folder在该文件夹的子文件夹中创建一个包含我的电子表格(.xlsx 文件)的文件夹
  2. create a gcp project创建一个 gcp 项目
  3. enable google drive api on the project在项目上启用谷歌驱动器 api
  4. enable google sheets api on the project在项目上启用 google sheets api
  5. create a service account on the project with a basic owner IAM role使用基本所有者 IAM 角色在项目上创建服务帐户
  6. take the service key json and place it locally on my computer to reach using python获取服务密钥 json 并将其本地放置在我的计算机上以使用 python 访问
  7. take the email address for the service key and share it with the main folder and data sets to allow the service key to have access.取服务密钥的email地址,共享到主文件夹和数据集,让服务密钥可以访问。
  8. run the following code to try and see a list of all spread sheets my service account has access to:运行以下代码以尝试查看我的服务帐户有权访问的所有电子表格的列表:
import gspread

# open credentials file and connect to google drive
gc = gspread.service_account(filename='credentials.json')

# list all spread sheets with access from credentials (returning empty list)
gc.list_spreadsheet_files()

My result is always the same, an empty list suggesting that my service account has access to no spread sheets, but when I look at my.xlsx files they all say that the service account email has been added as an editor.我的结果总是一样的,一个空列表表明我的服务帐户无法访问任何电子表格,但是当我查看 my.xlsx 文件时,他们都说服务帐户 email 已被添加为编辑器。

What could be the purpose of this and what would a good solution be?这样做的目的是什么,好的解决方案是什么?

I believe your goal is as follows.我相信你的目标如下。

  • Unfortunately, it seems that gc.list_spreadsheet_files() returns only Google Spreadsheet.不幸的是,似乎gc.list_spreadsheet_files()只返回谷歌电子表格。 So, in your expected goal, from your reply, I understood that you wanted to get the file list of both Google Spreadsheet files and Microsoft Excel files.因此,在您的预期目标中,从您的回复中,我了解到您想要同时获取 Google 电子表格文件和 Microsoft Excel 文件的文件列表。

In this case, how about the following sample script?在这种情况下,下面的示例脚本怎么样?

Sample script:示例脚本:

import gspread
from googleapiclient.discovery import build

gc = gspread.service_account(filename='credentials.json')
service = build("drive", "v3", credentials=gc.auth)

fileList = {"spreadsheet": [], "excel": []}
pageToken = ""
while pageToken is not None:
    res = service.files().list(q="(mimeType='application/vnd.google-apps.spreadsheet' or mimeType='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') and trashed=false", fields="nextPageToken, files(id,name,mimeType)", pageSize=1000, pageToken=pageToken, corpora="allDrives", includeItemsFromAllDrives=True, supportsAllDrives=True).execute()
    for e in res.get("files", []):
        if e["mimeType"] == "application/vnd.google-apps.spreadsheet":
            del e["mimeType"]
            fileList["spreadsheet"].append(e)
        elif e["mimeType"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
            del e["mimeType"]
            fileList["excel"].append(e)
    pageToken = res.get("nextPageToken")

print(fileList)

Testing:测试:

When this script is run, the following result is obtained.运行此脚本时,将获得以下结果。

{
  'spreadsheet': [{'id': '###', 'name': '###'}, {'id': '###', 'name': '###'},,,],
  'excel': [{'id': '###', 'name': '###'}, {'id': '###', 'name': '###'},,,]
}
  • By this, you can see the Google Spreadsheet files and the Microsoft Excel files.由此,您可以看到 Google Spreadsheet 文件和 Microsoft Excel 文件。

Reference:参考:

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

相关问题 将 Kubernetes 服务帐号连接到 Google Cloud 服务帐号 - Connect Kubernetes service account to Google Cloud service account 作为浏览器上的服务帐户向谷歌云进行身份验证 - Authenticate to google cloud as a service account on browser 存储在 AWS SecretManage 中的 Google 服务帐户凭据 - Google Service Account credentials stored in AWS SecretManage 通过服务帐户访问 Google Cloud Storage - Google Cloud Storage access via service account NodeJS 谷歌云服务帐户不记名令牌 - NodeJS Google Cloud Service Account Bearer Token 在 docker 图片上验证 Google Cloud 服务帐户 - Authenticate Google Cloud service account on docker image 如何验证谷歌服务帐户凭据 - How to authenticate google service account credentials 是否可以从已被授权访问 GS 存储桶的谷歌帐户授权谷歌服务帐户? - is it possible to authorize google service account from google account that is already authorized to have access on GS buckets? 从 Google 服务帐户模拟 Azure 服务主体 - Impersonate Azure Service Principal from a Google Service Account 如何删除由服务帐户创建的 Google 表格? - How can I delete a Google Sheets created by a service account?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM