Google 驱动器 API 数据与 UI 中的数据不匹配
Google drive API data doesn't match data in UI
我正在尝试连接到 Google 驱动器以下载供 Web 应用程序使用的文件。
我正在使用在 Google 云控制台中生成的 Service Accounts
密钥。
当我用 API 列出文件时,我只得到 1 个文件夹,但是当我查看 UI 时,我看到一些文件和 1 个文件夹(不是我用 [=19= 看到的那个) ]).
这是我使用的代码,表明我做错了什么?
from google.oauth2 import service_account
from googleapiclient.discovery import build
def init_connection():
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
return credentials
def list_files():
creds = init_connection()
drive_service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = drive_service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute(
print(results)
这个答案怎么样?
问题和解决方法:
来自 When I list the files with the API I get only 1 folder, but when I look at the UI I see a few files and 1 folder (not the one that I see with the API).
,我认为您可能已经将从服务帐户检索到的结果值与您的 Google 驱动器中显示的结果值进行了比较。如果是这样,则服务帐户的驱动器与您的 Google 驱动器不同。我认为这可能是您遇到问题的原因。
为了使用服务帐户检索 Google 驱动器中的文件和文件夹,以下解决方法如何?
解决方法:
在此解决方法中,首先,请使用服务帐户的电子邮件共享 Google 驱动器中的示例文件夹。并且,使用以下脚本检索文件列表。
示例脚本:
from google.oauth2 import service_account
from googleapiclient.discovery import build
def init_connection():
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
# or, the following script.
# SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
# credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
return credentials
def list_files():
creds = init_connection()
drive_service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
folderId = '###' # Please set the folder ID of the shared folder with the service account.
results = drive_service.files().list(pageSize=10, fields="nextPageToken, files(id, name)", q="'" + folderId + "' in parents").execute()
print(results)
参考文献:
我正在尝试连接到 Google 驱动器以下载供 Web 应用程序使用的文件。
我正在使用在 Google 云控制台中生成的 Service Accounts
密钥。
当我用 API 列出文件时,我只得到 1 个文件夹,但是当我查看 UI 时,我看到一些文件和 1 个文件夹(不是我用 [=19= 看到的那个) ]).
这是我使用的代码,表明我做错了什么?
from google.oauth2 import service_account
from googleapiclient.discovery import build
def init_connection():
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
return credentials
def list_files():
creds = init_connection()
drive_service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
results = drive_service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute(
print(results)
这个答案怎么样?
问题和解决方法:
来自 When I list the files with the API I get only 1 folder, but when I look at the UI I see a few files and 1 folder (not the one that I see with the API).
,我认为您可能已经将从服务帐户检索到的结果值与您的 Google 驱动器中显示的结果值进行了比较。如果是这样,则服务帐户的驱动器与您的 Google 驱动器不同。我认为这可能是您遇到问题的原因。
为了使用服务帐户检索 Google 驱动器中的文件和文件夹,以下解决方法如何?
解决方法:
在此解决方法中,首先,请使用服务帐户的电子邮件共享 Google 驱动器中的示例文件夹。并且,使用以下脚本检索文件列表。
示例脚本:
from google.oauth2 import service_account
from googleapiclient.discovery import build
def init_connection():
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE)
# or, the following script.
# SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
# credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
return credentials
def list_files():
creds = init_connection()
drive_service = build('drive', 'v3', credentials=creds)
# Call the Drive v3 API
folderId = '###' # Please set the folder ID of the shared folder with the service account.
results = drive_service.files().list(pageSize=10, fields="nextPageToken, files(id, name)", q="'" + folderId + "' in parents").execute()
print(results)