使用 Python 在 Cloud Function 中创建工作表 API 身份验证
Create Sheets API Authentication in Cloud Function with Python
我希望在云功能中使用 Google 表格 API,它将 运行 来自我帐户的默认服务帐户,我正在 Python。但是,我只在本地验证过 Sheets 库,使用这段代码:
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
def gen_creds(path_to_secret: str, rw_vs_ro: str):
"""
Generate the needed credentials to work with the Sheets v4 API based on your secret
json credentials file.
:param path_to_secret: The file path to your credentials json file
:param rw_vs_ro: A string, 'r_o' or 'r_w', representing whether creds should be readonly or readwrite
:return: The built service variable
"""
if rw_vs_ro == 'r_o':
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
creds_nm = 'readonly_token.json'
else:
scopes = ['https://www.googleapis.com/auth/spreadsheets']
creds_nm = 'readwrite_token.json'
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(creds_nm):
creds = Credentials.from_authorized_user_file(creds_nm, scopes)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
path_to_secret, scopes)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(creds_nm, 'w') as token:
token.write(creds.to_json())
return build('sheets', 'v4', credentials=creds)
而且我不完全确定如何将其转换为云函数可以理解的内容,因为云函数不会像我一样 运行ning,并且缺少相同类型的 os 我可以在本地访问的路径。如果能深入了解这里的翻译过程,我将不胜感激——我只能在 JS 中找到示例,这对我的目标来说并不完美。然后,我很想了解如何在 GCP 的云函数中实际实现此代码。谢谢!
部署云函数时,您的主代码将有权访问该函数中部署的所有文件。这意味着您需要做的就是在部署包时包含 readwrite_token.json/readonly_token.json 文件。
一旦完成,而不是简单地将令牌文件作为字符串传递,因为函数的目录可能与当前工作目录不同,您必须正确包含此 GCP Function Filesystem documentation 中指定的文件
此外,您不能在 Cloud Function 环境中使用 InstalledAppFlow,因为此流程适用于桌面 os 环境,因此最好祈祷该块永远不会被执行或替换为不同的流程。
实际上,我最终找到了这个问题的简单答案——在 GCP 中为 Python 生成这些凭据非常容易! gen_creds
的确切替换方法是:
import google.auth
from googleapiclient.discovery import build
def gen_creds(rw_vs_ro: str):
"""
Generate the service credentials to be used to query a google sheet
:param rw_vs_ro: A string, 'r_o' or 'r_w', representing whether creds should be readonly or readwrite
:return: The built service variable
"""
if rw_vs_ro == 'r_o':
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
if rw_vs_ro == 'r_w':
scopes = ['https://www.googleapis.com/auth/spreadsheets']
creds, project = google.auth.default(scopes=scopes)
service = build('sheets', 'v4', credentials=creds)
return service
希望这对其他人和我一样有帮助!
我希望在云功能中使用 Google 表格 API,它将 运行 来自我帐户的默认服务帐户,我正在 Python。但是,我只在本地验证过 Sheets 库,使用这段代码:
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
def gen_creds(path_to_secret: str, rw_vs_ro: str):
"""
Generate the needed credentials to work with the Sheets v4 API based on your secret
json credentials file.
:param path_to_secret: The file path to your credentials json file
:param rw_vs_ro: A string, 'r_o' or 'r_w', representing whether creds should be readonly or readwrite
:return: The built service variable
"""
if rw_vs_ro == 'r_o':
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
creds_nm = 'readonly_token.json'
else:
scopes = ['https://www.googleapis.com/auth/spreadsheets']
creds_nm = 'readwrite_token.json'
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists(creds_nm):
creds = Credentials.from_authorized_user_file(creds_nm, scopes)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
path_to_secret, scopes)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(creds_nm, 'w') as token:
token.write(creds.to_json())
return build('sheets', 'v4', credentials=creds)
而且我不完全确定如何将其转换为云函数可以理解的内容,因为云函数不会像我一样 运行ning,并且缺少相同类型的 os 我可以在本地访问的路径。如果能深入了解这里的翻译过程,我将不胜感激——我只能在 JS 中找到示例,这对我的目标来说并不完美。然后,我很想了解如何在 GCP 的云函数中实际实现此代码。谢谢!
部署云函数时,您的主代码将有权访问该函数中部署的所有文件。这意味着您需要做的就是在部署包时包含 readwrite_token.json/readonly_token.json 文件。 一旦完成,而不是简单地将令牌文件作为字符串传递,因为函数的目录可能与当前工作目录不同,您必须正确包含此 GCP Function Filesystem documentation 中指定的文件 此外,您不能在 Cloud Function 环境中使用 InstalledAppFlow,因为此流程适用于桌面 os 环境,因此最好祈祷该块永远不会被执行或替换为不同的流程。
实际上,我最终找到了这个问题的简单答案——在 GCP 中为 Python 生成这些凭据非常容易! gen_creds
的确切替换方法是:
import google.auth
from googleapiclient.discovery import build
def gen_creds(rw_vs_ro: str):
"""
Generate the service credentials to be used to query a google sheet
:param rw_vs_ro: A string, 'r_o' or 'r_w', representing whether creds should be readonly or readwrite
:return: The built service variable
"""
if rw_vs_ro == 'r_o':
scopes = ['https://www.googleapis.com/auth/spreadsheets.readonly']
if rw_vs_ro == 'r_w':
scopes = ['https://www.googleapis.com/auth/spreadsheets']
creds, project = google.auth.default(scopes=scopes)
service = build('sheets', 'v4', credentials=creds)
return service
希望这对其他人和我一样有帮助!