用于翻译的 Google API 密钥
从程序中进行简单文本翻译需要什么键(如果有)?
注意:我不托管任何网站。
what key (if any) do I require for simple text translation from program?
Note: I don't host any website.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取 Google Translate API 密钥
要使用 Google API,您首先需要一个 Google Cloud 帐户。
所有 GCP API 的 API 密钥均相同。说明如下:https://cloud.google.com/docs/authentication/api-keys
激活 Google Translate API
在您的项目中使用 Google API 之前,您必须先激活它。转到侧面菜单并选择 API 和 API。服务选项:
现在您将看到一个屏幕,其中包含有关您已激活的 API 的统计信息。如果您按照上述步骤从头开始创建项目,则还没有任何数据,如您所见:
单击上方按钮“启用 API 和服务”以继续激活过程API 的。这会将我们带到一个搜索框,我们必须在其中查找我们感兴趣的 API。在本例中,我们要使用 Google Translate API。在搜索框中输入“翻译”,然后单击结果 Cloud Translate API:
这会将我们带到一个包含 Cloud Translation API 描述的屏幕。单击启用按钮以激活我们项目中的 API:
我们已经激活了 Google Cloud Translation API。我们就快完成了...
为 Google 翻译创建新的 API 密钥
激活 API 后,让我们看看如何生成新的密钥 API 以便能够使用此服务。我们必须再次转到侧面菜单并选择“凭据”选项:
在此屏幕上,我们看到一个带有下拉菜单的按钮和文本“创建凭据”。不要点击按钮!相反,单击按钮右侧的箭头打开下拉菜单,然后选择 API 密钥选项。
这将创建新的密钥 API。如果需要,您可以复制它,但稍后可以访问它:
Google Cloud 为我们提供了一个新的 Key API,以便将 Google Translate 与我们的第三方应用程序结合使用。
如何限制我们的 API 密钥以保护和限制其使用
要通过使用 Google Cloud Translation API(或 Google Translate,两者相同)来控制 Google Cloud 的成本,我们可以做两件事:限制您可以使用我们刚刚创建的 API 密钥的位置,或限制使用服务本身所允许的配额。
在上面的屏幕截图中,如果单击“限制密钥”按钮,您将转到以下 API 密钥限制屏幕:
您可以选择通过 HTTP 引用来限制 API 密钥,这意味着您只能使用特定域名的 API 密钥调用 Google Cloud Translate API。
您必须在选择 HTTP 引荐来源网址选项时出现的文本框中添加有效域名。示例域名 https://google.com/*。
另一方面,转到菜单 API 的 &服务 → 仪表板 → 云翻译 API → 配额,您将在其中找到一个名为“字符”的框。您可以在此处修改 Google Cloud Translation API 的配额限制并根据需要减少配额限制。
在撰写本文时,Google Cloud Translation API 的价格为每百万翻译字符 20 美元,因此请自行计算。
就这样!您已经准备好 API 密钥,可以与您想要的应用程序一起使用。一开始这个过程有点麻烦,但是按照步骤操作就可以毫无问题地获得 API Key。
Getting a Google Translate API Key
To use the Google API, you first need a Google Cloud account.
API keys are the same for all GCP APIs. Instructions here: https://cloud.google.com/docs/authentication/api-keys
Activate the Google Translate API
Before you can use a Google API in your project, you have to activate it. Go to the side menu and select the APIs & Services option:
Now you will see a screen with statistics about the APIs that you have activated. If you created the project from scratch by following the steps above, you won’t have any data yet, as you can see:
Click on the upper button Enable APIs and services to continue with the activation process of the API. This takes us to a search box where we have to look for the API we’re interested in. In this case, we want to use the Google Translate API. Type translate in the search box and click on the result Cloud Translate API:
This brings us to a screen with the description of the Cloud Translation API. Click on the Enable button to activate the API in our project:
We already have the Google Cloud Translation API activated. We’re almost there…
Create a new API Key for Google Translate
After activating the API, let’s see how to generate a new Key API to be able to use this service. We have to go to the side menu again and select the Credentials option:
On this screen we see a button with a drop-down and the text Create credentials. Don’t click on the button! Instead, open the drop-down by clicking on the arrow to the right of the button and select the API Key option.
This creates the new Key API. You can copy it if you want, although you can access it later:
Google Cloud provides us with a new Key API to use Google Translate with our third-party applications.
How to Restrict Our API Key to Protect and Limit Its Use
To control the cost of Google Cloud by using the Google Cloud Translation API (or Google Translate, which is the same), we can do two things: restrict where you can use the API Key that we just created or limit the allowed quota to use the service itself.
In the screenshot above, if you click on the Restrict Key button you will go to the API Key restriction screen that follows:
There you can select to restrict the API Key by HTTP referrers, which means that you can only make calls to the Google Cloud Translate API using the API Key from certain domain names.
You must add the valid domain names in the text box that appears when selecting the HTTP referrers option. Sample domain name https://google.com/*.
On the other hand, go to the menu API’s & Services → Dashboard → Cloud Translation API → Quotas and there you will find a box called Characters. There you can modify the quota limits of the Google Cloud Translation API and reduce them, if necessary.
The price for Google Cloud Translation API at the time of writing this post is 20 USD per million translated characters, so make your numbers.
And that’s all! You already have your API Key ready to be used with the application you want. The process is a bit cumbersome at first, but following the steps you’ll get the API Key without problems.
使用 Google Translate API 需要 API 密钥,您可以从 Google API 获取 API 密钥控制台。
有关更多详情,请参阅开发人员指南。
An API key is required to use the Google Translate API and you can get yours from the Google APIs Console.
For further details, check the Developer's Guide.