我不想使用 gcloud cli 或环境变量设置项目,我想以编程方式执行此操作。我正在尝试使用 client_options,但这不起作用:
from google.cloud import translate_v2 as translate
def translate_text(target: str, text: str):
translate_client = translate.Client(
client_options={
"quota_project_id": "my-proj-id",
}
)
...
我收到错误:
Cloud Translation API has not been used in project <A DIFFERENT PROJECT>
。
如何在构造函数中将其设置为特定项目?
我找到了同一 API 的另一个代码示例(令人烦恼的是,我知道其中的区别是什么以及我应该使用哪一个)。
但这也不起作用,当我全局设置时它会起作用:
gcloud auth application-default set-quota-project my-project
,但我不想这样做。我无法在运行此容器化应用程序的地方运行 gcloud。
# Imports the Google Cloud Translation library
from google.cloud import translate
# Initialize Translation client
def translate_text(
text: str = "YOUR_TEXT_TO_TRANSLATE", project_id: str = "YOUR_PROJECT_ID"
) -> translate.TranslationServiceClient:
"""Translating Text."""
client = translate.TranslationServiceClient(
client_options={
"quota_project_id": "my-proj",
}
)
location = "global"
parent = f"projects/{project_id}/locations/{location}"
# Translate text from English to French
# Detail on supported types can be found here:
# https://cloud.google.com/translate/docs/supported-formats
request = {
"parent": parent,
"contents": [text],
"mime_type": "text/plain", # mime types: text/plain, text/html
"source_language_code": "en-US",
"target_language_code": "fr",
}
response = client.translate_text(**request)
# Display the translation for each input text provided
for translation in response.translations:
print(f"Translated text: {translation.translated_text}")
return response
translate_text("hello", "my-proj")
我得到:
google.api_core.exceptions.PermissionDenied: 403 Cloud Translation API has not been used in project <MY SERVICE ACCOUNTS PROJECT> before or it is disabled.
此 GSA 与启用了 API 的项目位于不同的项目中,我无法更改这一点。我现在可以使用 gcloud cli,如何在代码中执行此操作?
这是我让它发挥作用的唯一方法。我发誓其他客户让设置项目变得很容易,所以我猜可能只是这个 api 或者我正在使用旧的库版本或其他东西。
也许我应该使用发现 API 并通过它构建客户端?但我找不到例子。
此外,还有很多可怕的错误,导致解决这个问题所需的时间比应有的时间要长。如果服务帐户根本没有项目的权限,我会收到此错误,这是错误的,非常荒谬:
google.api_core.exceptions.InvalidArgument:400 无效的“父级”。; 资源名称项目 ID 无效。标识符必须仅包含 小写字母、数字、句点、冒号或连字符。;资源 类型:位置
# Imports the Google Cloud Translation library
from google.cloud import translate
import google.auth
# Initialize Translation client
def translate_text(
text: str = "YOUR_TEXT_TO_TRANSLATE", project_id: str = "YOUR_PROJECT_ID"
) -> translate.TranslationServiceClient:
"""Translating Text."""
# Manually build creds with quota_project_id set
credentials, _ = google.auth.default(quota_project_id=project_id)
client = translate.TranslationServiceClient(
credentials=credentials,
# This is actually not required if creds are pass in it seems
client_options={
"quota_project_id": project_id,
},
)
location = "global"
parent = f"projects/{project_id}/locations/{location}"
# Translate text from English to French
# Detail on supported types can be found here:
# https://cloud.google.com/translate/docs/supported-formats
request = {
"parent": parent,
"contents": [text],
"mime_type": "text/plain", # mime types: text/plain, text/html
"source_language_code": "en-US",
"target_language_code": "fr",
}
response = client.translate_text(**request)
# Display the translation for each input text provided
for translation in response.translations:
print(f"Translated text: {translation.translated_text}")
return response
translate_text("hello", "my-project")