DBC*_*igo 5 python authentication google-api google-drive-api google-cloud-platform
我们公司正在处理来自 Google Cloud Platform 的 Google Sheets(在 Google Drive 中)的数据,我们在身份验证方面遇到了一些问题。
我们需要在两个不同的地方运行代码来调用 Google Drive 的 API:在 Google Compute Engine 的生产环境中,以及在开发环境中,即在我们开发人员的笔记本电脑上本地。
我们公司对凭据非常严格,不允许下载服务帐户凭据 JSON 密钥(这是更好的做法并提供更高的安全性)。似乎 GCP 的所有文档都说只需下载服务帐户的 JSON 密钥并使用它。或者 Google APIs/Developers docs 说要创建一个 OAuth2 客户端 ID 并像这里一样下载它的关键。
他们经常使用这样的代码:
from google.oauth2 import service_account
SCOPES = ['https://www.googleapis.com/auth/sqlservice.admin']
SERVICE_ACCOUNT_FILE = '/path/to/service.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
Run Code Online (Sandbox Code Playgroud)
但是我们不能(或者只是不想)下载我们的服务帐户 JSON 密钥,所以如果我们只是按照文档进行操作,我们就会陷入困境。
对于 Google Compute Engine 环境,我们已经能够使用 GCP 应用程序默认凭据 (ADC) 进行身份验证——即没有明确指定要在代码中使用的凭据并让客户端库“正常工作”——只要确保VM 是用正确的范围创建的https://www.googleapis.com/auth/drive,默认的计算服务帐户电子邮件被授予需要访问的工作表的权限 - 这在文档here中进行了解释。你可以这样做;
from googleapiclient.discovery import build
service = build('sheets', 'v4')
SPREADSHEET_ID="<sheet_id>"
RANGE_NAME="A1:A2"
s = service.spreadsheets().values().get(
spreadsheetId=SPREADSHEET_ID,
range=RANGE_NAME, majorDimension="COLUMNS"
).execute()
Run Code Online (Sandbox Code Playgroud)
但是,我们如何为开发做到这一点,即在我们开发人员的笔记本电脑上本地?同样,无需下载任何 JSON 密钥,最好使用最“可行”的方法?
通常我们gcloud auth application-default login用来创建 Google 客户端库使用的默认应用程序凭据,这些凭据“可以正常工作”,例如用于 Google Storage。但是,这不适用于 GCP 之外的 Google API,例如 Google Drive APIservice = build('sheets', 'v4')失败并显示以下错误:“请求的身份验证范围不足。”。然后我们尝试了各种解决方案,例如:
credentials, project_id = google.auth.default(scopes=["https://www.googleapis.com/auth/drive"])
Run Code Online (Sandbox Code Playgroud)
和
credentials, project_id = google.auth.default()
credentials = google_auth_oauthlib.get_user_credentials(
["https://www.googleapis.com/auth/drive"], credentials._client_id, credentials._client_secret)
)
Run Code Online (Sandbox Code Playgroud)
以及更多...当尝试对 Google Drive API 进行身份验证时,所有这些都会导致我们无法解决的无数错误/问题:(
有什么想法吗?
简化开发环境身份验证的一种方法是使用服务帐户模拟。
这是关于使用服务帐户模拟的博客,包括这样做的好处。@johnhanley(博文的作者)是一个很棒的人,并且也有很多非常有用的答案!
为了能够让您的本地计算机针对 Google Drive API 进行身份验证,您需要在本地计算机上创建模拟服务帐户的默认应用程序凭据,并应用您想要访问的 API 所需的范围。
为了能够模拟服务帐户,您的用户必须具有该角色roles/iam.serviceAccountTokenCreator。此角色可以应用于整个项目或单个服务帐户。
您可以使用 来gcloud执行此操作:
gcloud iam service-accounts add-iam-policy-binding [COMPUTE_SERVICE_ACCOUNT_FULL_EMAIL] \
--member user:[USER_EMAIL] \
--role roles/iam.serviceAccountTokenCreator
Run Code Online (Sandbox Code Playgroud)
完成后创建本地凭据:
gcloud auth application-default login \
--scopes=openid,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/accounts.reauth \
--impersonate-service-account=[COMPUTE_SERVICE_ACCOUNT_FULL_EMAIL]
Run Code Online (Sandbox Code Playgroud)
这将解决您遇到的范围错误。在 Drive API 范围之外添加的三个额外范围是gcloud auth application-default login适用且需要的默认范围。
如果您应用范围而不进行模拟,那么在尝试进行身份验证时将会收到如下错误:
HttpError: <HttpError 403 when requesting https://sheets.googleapis.com/v4/spreadsheets?fields=spreadsheetId&alt=json returned "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the sheets.googleapis.com. We recommend configuring the billing/quota_project setting in gcloud or using a service account through the auth/impersonate_service_account setting. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.">
Run Code Online (Sandbox Code Playgroud)
设置凭据后,您可以使用在本地计算机上的 Google Compute Engine 上运行的相同代码:)
注意:还可以为所有 gcloud 命令设置模拟:
gcloud config set auth/impersonate_service_account [COMPUTE_SERVICE_ACCOUNT_FULL_EMAIL]
Run Code Online (Sandbox Code Playgroud)
通过模拟服务帐户在本地计算机上创建默认应用程序凭据是验证开发代码的一种巧妙方法。这意味着该代码将具有与其模拟的服务帐户完全相同的权限。如果这是在生产中运行代码的同一个服务帐户,您就知道开发中的代码与生产中的代码运行方式相同。这也意味着您无需创建或下载任何服务帐户密钥。
| 归档时间: |
|
| 查看次数: |
642 次 |
| 最近记录: |