No results found
We couldn't find anything using that term, please try searching for something else.
本文档介绍了如何以程序化方式向 Speech-to-Text 进行身份验证。如何向 Speech-to-Text 进行身份验证取决于您用于访问 API 的界面以及运行代码的环境。 如需详细了解 Google Cloud 身份验证,请参阅身份验证概览。 API 访问权限
本文档介绍了如何以程序化方式向 Speech-to-Text 进行身份验证。如何向 Speech-to-Text 进行身份验证取决于您用于访问 API 的界面以及运行代码的环境。
如需详细了解 Google Cloud 身份验证,请参阅身份验证概览。
Speech – to – text 支持程序化访问。您可以通过以下方式访问 API :
Speech-to-Text 客户端库提供以程序化方式向 Speech-to-Text 进行身份验证的高级语言支持。
为了对 Google Cloud API 的调用进行身份验证,客户端库支持应用默认凭据 (ADC);这些库会在一组定义的位置查找凭据,并使用这些凭据对发送到 API 的请求进行身份验证。借助 ADC,您可以在各种环境(例如本地开发或生产环境)中为您的应用提供凭据,而无需修改应用代码。
您可以使用 gcloud CLI 凭据或使用应用默认凭据向 Speech-to-Text API 进行身份验证。如需详细了解 rest 请求身份验证,请参阅使用 rest 时进行身份验证。如需了解凭据类型,请参阅 gcloud CLI 凭据和 ADC 凭据。
向 ADC 提供凭据的一种方法是使用 gcloud CLI 将您的用户凭据插入凭据文件中。此文件位于本地文件系统上,ADC 可以找到它。然后,ADC 使用提供的用户凭据对请求进行身份验证。此方法通常用于本地开发。
如果您使用此方法,则在尝试向 Speech-to-Text 进行身份验证时可能会遇到身份验证错误。如需详细了解此错误及其解决方法,请参阅用户凭据不起作用。
设置身份验证的方式取决于代码在其中运行的环境 。
以下是最常用的身份验证设置选项。如需了解有关身份验证的更多选项和信息,请参阅身份验证方法。
您可以通过以下方式为本地开发环境设置凭据 :
在本地环境中设置应用默认凭据 (ADC):
Install the Google Cloud CLI, then
initialize it by running the following command:
gcloud init
If you’re using a local shell, then create local authentication credentials for your user
account:
gcloud auth application - default login
You don’t need to do this if you’re using Cloud Shell.
note: If the gcloud CLI prints a warning that your account
doesn’t have the serviceusage.services.use
permission, then some
gcloud CLI commands and client libraries might not work. Ask an
administrator to grant you the Service Usage Consumer IAM role
(role / serviceusage.serviceUsageConsumer
), then run the following command:
gcloud auth application - default set - quota - project
登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。
如需详细了解如何在本地环境中使用 ADC,请参阅本地开发环境。
从命令行发出 rest 请求时,您可以通过在发送该请求的命令中添加 gcloud auth print-access-token
来使用 gcloud CLI 凭据。
以下示例列出了指定项目的服务账号。您可以对任何 rest 请求使用相同的模式。
在使用任何请求数据之前,请先进行以下替换:
如需发送您的请求,请展开以下选项之一:
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H " x - goog - user - project : " \
"https://iam.googleapis.com/v1/project//serviceAccounts"
执行以下命令:
$cred = gcloud auth print-access-token
$ header = @ { " Authorization " = " Bearer $ cred " ; " x - goog - user - project " = " " }
invoke - WebRequest `
-Method GET `
-header $ header `
-Uri "https://iam.googleapis.com/v1/project//serviceAccounts" | Select-Object -Expand Content
如需详细了解如何使用 rest 和 gRPC 进行身份验证,请参阅使用 rest 时进行身份验证。如需了解本地 ADC 凭据与 gcloud CLI 凭据之间的区别,请参阅 gcloud CLI 身份验证配置和 ADC 配置。
如需对 Google Cloud 上运行的工作负载进行身份验证,请使用关联到代码在其中运行的计算资源(例如 Compute Engine 虚拟机 [ VM ] 实例)的服务账号的凭据。对于在 Google Cloud 计算资源上运行的代码,这是首选的身份验证方法 。
对于大多数服务,您必须在创建将运行代码的资源时关联服务账号;您之后无法添加或替换服务账号。Compute Engine 是一个例外,它允许您随时将服务账号关联到虚拟机实例。
使用 gcloud CLI 创建服务账号并将其关联到您的资源:
Install the Google Cloud CLI, then
initialize it by running the following command:
gcloud init
Set up authentication :
Create the service account:
gcloud iam service-accounts create
replace with a name for the service account .
To provide access to your project and your resources, grant a role to the service account:
gcloud project add - iam - policy - bind --member="serviceAccount:@.iam.gserviceaccount.com" --role=
replace the follow :
: the name of the service account
: the project ID where you created the service account
: the role to grantnote: The --role
flag affects which resources the service account can access in your
project. You can revoke these roles or grant additional roles later.
In production environments, do not grant the Owner, Editor, or Viewer roles. Instead, grant a
predefined role or
custom role that meets your needs.
Grant the required role to the principal that
will attach the service account to other resources.
gcloud iam service-accounts add - iam - policy - bind @.iam.gserviceaccount.com --member="user:" --role=roles/iam.serviceAccountUser
replacethe following:
: the name of the service account
: the project ID where you created the service account
: the email address for a Google Account创建将运行代码的资源,并将服务账号关联到该资源。例如,如果您使用 Compute Engine:
create a Compute Engine instance . configure the instance as follow :
如需详细了解如何向 Google API 进行身份验证,请参阅身份验证方法。
如需在 Google Cloud 外部设置身份验证,首选方法是使用工作负载身份联合。如需了解详情,请参阅身份验证文档中的本地或其他云服务提供商。