GCP Sheets/Docs API 未在 iframe 中显示文件
GCP Sheets/Docs API not displaying files in iframe
我有一个项目,每次保存一个新对象时,它也会创建空白 Google Doc 和 Google Sheet 作为用户可以使用的额外资源。
为此,我在 Google Cloud Platform 中创建了一个服务帐户,生成了 .json,授予了文档中所述的权限(浏览器、服务帐户管理员、IAM 工作负载身份池管理员、工作负载身份用户)。
还创建了一个 API 密钥。
有了它,我可以成功地创建、删除、列出所有文件并重命名特定文件(下面的脚本),但我无法加载包含文档和电子表格本身的 iframe。我在iframe的src中尝试加载的地址:
https://docs.google.com/document/d/DOC_ID?key=APIKEY
https://docs.google.com/spreadsheets/d/SHEET_ID/?key=APIKEY
https://docs.googleapis.com/v1/documents/DOC_ID?key=APIKEY
https://sheets.googleapis.com/v4/spreadsheets/SHEET_ID?key=APIKEY
前两个地址加载“您需要访问权限 - 请求访问权限,或切换到具有访问权限的帐户。” Google 消息(此处为屏幕截图 "You need access" issue)。
另外两个抛出 401 -“请求缺少必需的身份验证凭据。需要 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。”。
问题:
如何在 Google 的标准 iframe(带有用户界面、工具栏等)上显示这些文件?
有没有办法像个人 Google Drive 一样在 Google 云平台网站上查看服务帐户创建的所有文件?我试过 drive.google.com 但它是空的,但我可以使用 Python.
列出它们
我试图让它与 AWS 中的 Workload Identity Federation 一起工作,所以我创建了池,使用我的 AWS ID 将 AWS 设置为提供者,并从“CONNECTED SERVICE ACCOUNTS”中选择了服务账户。是否有关于 AWS 配置方面的指南?暂时搁置了,因为我不知道下一步。
# GOOGLE DOC CREATION
def document_create(request, obj):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = ['https://www.googleapis.com/auth/documents']
body = {
'title': obj.title,
}
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('docs', 'v1', credentials=creds)
document = service.documents().create(body=body).execute()
# GOOGLE SHEET CREATION
def spreadsheet_create(request, obj):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
spreadsheet = {
'properties': {
'title': obj.title,
}
}
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)
spreadsheet = service.spreadsheets().create(body=spreadsheet, fields='spreadsheetId').execute()
# LIST ALL FILES
def index(request, template='gcp/index.html'):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = [] # WORKS WITHOUT ANY SCOPE SOMEHOW
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)
results = service.files().list().execute()
drive_list = results.get('files', [])
# RENAME A FILE
def rename(request):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = [] # ALSO WORKS WITHOUT ANY SCOPE SOMEHOW
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)
try:
service.files().update(fileId=file_id, body={'name': new_file_name}).execute()
except Exception as e:
pass
(这是我第一次 post 来这里,如有不尽如人意之处,敬请见谅。)
提前致谢!
您需要 share those documents to the public,才能在网页上提供服务。否则您只能将它们呈现为 HTML、XLSX 或 PDF 以供查看,但不能嵌入它们。
事实证明,使用表格和文档 API 创建的每个新文件默认都是私有的。
要使其可用,您只需添加指定类型 (user/domain)、角色和与所选类型对应的值的新权限。
这必须使用驱动器 API 完成。
我有一个项目,每次保存一个新对象时,它也会创建空白 Google Doc 和 Google Sheet 作为用户可以使用的额外资源。
为此,我在 Google Cloud Platform 中创建了一个服务帐户,生成了 .json,授予了文档中所述的权限(浏览器、服务帐户管理员、IAM 工作负载身份池管理员、工作负载身份用户)。
还创建了一个 API 密钥。
有了它,我可以成功地创建、删除、列出所有文件并重命名特定文件(下面的脚本),但我无法加载包含文档和电子表格本身的 iframe。我在iframe的src中尝试加载的地址:
https://docs.google.com/document/d/DOC_ID?key=APIKEY
https://docs.google.com/spreadsheets/d/SHEET_ID/?key=APIKEY
https://docs.googleapis.com/v1/documents/DOC_ID?key=APIKEY
https://sheets.googleapis.com/v4/spreadsheets/SHEET_ID?key=APIKEY
前两个地址加载“您需要访问权限 - 请求访问权限,或切换到具有访问权限的帐户。” Google 消息(此处为屏幕截图 "You need access" issue)。
另外两个抛出 401 -“请求缺少必需的身份验证凭据。需要 OAuth 2 访问令牌、登录 cookie 或其他有效的身份验证凭据。”。
问题:
如何在 Google 的标准 iframe(带有用户界面、工具栏等)上显示这些文件?
有没有办法像个人 Google Drive 一样在 Google 云平台网站上查看服务帐户创建的所有文件?我试过 drive.google.com 但它是空的,但我可以使用 Python.
列出它们我试图让它与 AWS 中的 Workload Identity Federation 一起工作,所以我创建了池,使用我的 AWS ID 将 AWS 设置为提供者,并从“CONNECTED SERVICE ACCOUNTS”中选择了服务账户。是否有关于 AWS 配置方面的指南?暂时搁置了,因为我不知道下一步。
# GOOGLE DOC CREATION
def document_create(request, obj):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = ['https://www.googleapis.com/auth/documents']
body = {
'title': obj.title,
}
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('docs', 'v1', credentials=creds)
document = service.documents().create(body=body).execute()
# GOOGLE SHEET CREATION
def spreadsheet_create(request, obj):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
spreadsheet = {
'properties': {
'title': obj.title,
}
}
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=creds)
spreadsheet = service.spreadsheets().create(body=spreadsheet, fields='spreadsheetId').execute()
# LIST ALL FILES
def index(request, template='gcp/index.html'):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = [] # WORKS WITHOUT ANY SCOPE SOMEHOW
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)
results = service.files().list().execute()
drive_list = results.get('files', [])
# RENAME A FILE
def rename(request):
SERVICE_ACCOUNT_FILE = '.json'
SCOPES = [] # ALSO WORKS WITHOUT ANY SCOPE SOMEHOW
creds = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)
try:
service.files().update(fileId=file_id, body={'name': new_file_name}).execute()
except Exception as e:
pass
(这是我第一次 post 来这里,如有不尽如人意之处,敬请见谅。)
提前致谢!
您需要 share those documents to the public,才能在网页上提供服务。否则您只能将它们呈现为 HTML、XLSX 或 PDF 以供查看,但不能嵌入它们。
事实证明,使用表格和文档 API 创建的每个新文件默认都是私有的。
要使其可用,您只需添加指定类型 (user/domain)、角色和与所选类型对应的值的新权限。
这必须使用驱动器 API 完成。