授权 Google Drive 服务帐户将 pandas df 写入 Google Sheets
Authorizing Google Drive service account to write pandas df to Google Sheets
我正在使用 Google Co.lab notebook 将 pandas
数据帧写入我个人 Google Drive 帐户中的 Google Sheet .
我已经使用 Google 驱动器 API 创建了一个服务帐户并创建了一个 API 密钥,它位于 Google 驱动器(My Drive/project/scrapers/utils/auth_key.json
).我想通过 Drive Services 进行身份验证,这样我就可以根据 .
使用 Drive API 到 move/write Sheets 进入特定文件夹
我在服务帐户身份验证方面遇到问题:
import os
import gspread
# Mount Google Drive
from google.colab import drive
drive.mount("/content/gdrive")
# Authenticate service account
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
filepath = os.path.expanduser('/content/gdrive/My Drive/project/utils/api/service_key.json')
credentials = ServiceAccountCredentials.from_json_keyfile_name(filepath, scope)
gc = gspread.authorize(credentials)
# Specify spreadsheet name and desired folder
title = 'test' # Spreadsheet name.
my_path = "/project/scrapers/csv_output" # File path
destFolderId = "/content/gdrive" + "/My Drive" + my_path # Full file path
# Write spreadsheet to desired folder
from apiclient import discovery
drive_service = discovery.build('drive', 'v3', credentials=credentials)
file_metadata = {
'name': title,
'mimeType': 'application/vnd.google-apps.spreadsheet',
'parents': [destFolderId]
}
file = drive_service.files().create(body=file_metadata).execute()
这会导致错误 <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.".
我认为这意味着我的服务帐户没有正确的权限设置,但经过几个小时的研究我不确定如何解决。我已将与服务帐户关联的电子邮件授予 Google Drive 文件夹的访问权限,并且服务帐户具有 Owner
权限。请帮忙!
挂载完成后
drive.mount('/content/gdrive')
文件可以像
一样访问
/content/gdrive/My Drive/Test_Data/elephant.jpg
为了
ServiceAccountCredentials.from_json_keyfile_name(filepath, scope)
验证 API 及其范围。
我正在使用 Google Co.lab notebook 将 pandas
数据帧写入我个人 Google Drive 帐户中的 Google Sheet .
我已经使用 Google 驱动器 API 创建了一个服务帐户并创建了一个 API 密钥,它位于 Google 驱动器(My Drive/project/scrapers/utils/auth_key.json
).我想通过 Drive Services 进行身份验证,这样我就可以根据
我在服务帐户身份验证方面遇到问题:
import os
import gspread
# Mount Google Drive
from google.colab import drive
drive.mount("/content/gdrive")
# Authenticate service account
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://spreadsheets.google.com/feeds']
filepath = os.path.expanduser('/content/gdrive/My Drive/project/utils/api/service_key.json')
credentials = ServiceAccountCredentials.from_json_keyfile_name(filepath, scope)
gc = gspread.authorize(credentials)
# Specify spreadsheet name and desired folder
title = 'test' # Spreadsheet name.
my_path = "/project/scrapers/csv_output" # File path
destFolderId = "/content/gdrive" + "/My Drive" + my_path # Full file path
# Write spreadsheet to desired folder
from apiclient import discovery
drive_service = discovery.build('drive', 'v3', credentials=credentials)
file_metadata = {
'name': title,
'mimeType': 'application/vnd.google-apps.spreadsheet',
'parents': [destFolderId]
}
file = drive_service.files().create(body=file_metadata).execute()
这会导致错误 <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "Insufficient Permission: Request had insufficient authentication scopes.".
我认为这意味着我的服务帐户没有正确的权限设置,但经过几个小时的研究我不确定如何解决。我已将与服务帐户关联的电子邮件授予 Google Drive 文件夹的访问权限,并且服务帐户具有 Owner
权限。请帮忙!
挂载完成后
drive.mount('/content/gdrive')
文件可以像
/content/gdrive/My Drive/Test_Data/elephant.jpg
为了
ServiceAccountCredentials.from_json_keyfile_name(filepath, scope)
验证 API 及其范围。