Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials 的解决方案
错误概述
当您在使用 Google BigQuery 时,可能会遇到以下错误信息:“Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials”。这表明您在尝试访问 Google Drive 的凭证时,被拒绝了访问权限。此错误通常是由于未正确设置授权凭证或未授予相应的访问权限导致的。
常见原因
- 未授权的凭证:您可能尚未为您的应用程序设置正确的 Google Cloud 凭证。
- 缺少必要的权限:在向 Google Drive 和 BigQuery API 发出请求时,您可能没有授予足够的权限。
- API 未启用:您可能未在 Google Cloud 项目中启用 Google Drive API 和 BigQuery API。
- 不正确的 OAuth 范围:在创建凭证时,未指定足够的 OAuth 范围。
- 服务账户权限不足:服务账户可能没有访问 Google Drive 的权限。
解决方法
方法 1: 设置应用程序默认凭证
- 打开终端或命令提示符。
- 执行以下命令以登录并设置应用程序默认凭证:
bash
gcloud auth application-default login --scopes=openid,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/bigquery - 注意:一旦您将默认凭证设置为授权用户,您将无法请求额外的范围。必须在授权用户激活期间请求附加范围。
方法 2: 为服务账户添加 OAuth 范围
- 使用以下命令为您的服务账户添加必要的 OAuth 范围:
bash
gcloud auth application-default login --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive.readonly,https://www.googleapis.com/auth/drive,https://www.googleapis.com/auth/bigquery - 确保您已为服务账户分配了足够的权限,以便访问 Google Drive 和 BigQuery。
方法 3: 在代码中指定凭证和范围
- 使用以下 Python 代码导入所需的库并创建凭证:
“`python
from google.cloud import bigquery
import google.auth
创建带有 Drive 和 BigQuery API 范围的凭证。
credentials, project = google.auth.default(
scopes=[
“https://www.googleapis.com/auth/drive”,
“https://www.googleapis.com/auth/bigquery”,
]
)
构造 BigQuery 客户端对象。
client = bigquery.Client(credentials=credentials, project=project)
“`
2. 确保在运行此代码之前,您已经为项目启用了 Google Drive 和 BigQuery API。
方法 4: 在云函数中声明 OAuth 范围
- 在创建云函数时,确保声明 OAuth 范围以启用 Drive 访问:
- 详细信息请参考 Google Cloud 官方文档。
- 通过适当的权限配置确保您有权访问所需的 API。
方法 5: 使用服务账户文件
- 使用以下 Python 代码从服务账户文件中创建凭证:
“`python
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
“service_account_file.json”,
scopes=[“https://www.googleapis.com/auth/drive”, “https://www.googleapis.com/auth/cloud-platform”, “https://www.googleapis.com/auth/bigquery”]
)
``service_account_file.json` 文件存在,并包含正确的服务账户信息。
2. 请确保
方法 6: 使用 App Engine 的凭证
- 使用以下代码创建 App Engine 凭证:
“`python
from google.auth import app_engine
from googleapiclient.discovery import build
creds = app_engine.Credentials(
scopes=[“https://www.googleapis.com/auth/userinfo.email”, “https://www.googleapis.com/auth/drive”]
)
创建 Drive API 客户端
drive_client = build(‘drive’, ‘v3’, credentials=creds)
“`
2. 这段代码将帮助您列出服务账户可访问的所有文件。
方法 7: 创建新的 Airflow 连接
- 如果您使用 Airflow,可以通过创建新的 Google Cloud 连接来解决问题。确保新连接的值与默认连接相同。
- 这可以通过 Airflow 的管理界面完成,确保新的连接可以正常工作。
预防提示
- 确保在 Google Cloud 控制台中启用所有必需的 API。
- 确认服务账户是否已被授予足够的权限以访问 Google Drive 和 BigQuery。
- 在进行任何 API 调用之前,仔细检查 OAuth 范围是否正确配置。
总结
通过以上方法,您应该能够解决“Forbidden: 403 Access Denied: BigQuery BigQuery: Permission denied while getting Drive credentials”这一错误。确保在设置凭证时,正确配置 OAuth 范围并授予必要的权限,以便顺利访问 Google Drive 和 BigQuery API。如果问题仍然存在,请检查 Google Cloud 控制台中的设置或参考官方文档获取更多帮助。

コメント