使用Google Cloud API进行开发

Google提供的api现已全部集成到了google clouds,使用起来比较麻烦。本文介绍如何使用Google的api进行开发

以Google Translation 谷歌翻译为例

 

第一步

创建一个Google账号,然后登陆https://console.cloud.google.com/

点创建项目

 

 

然后,选择你的创建项目

 

第二步

点击导航菜单,选择API和服务 -> 库

 

然后搜索google translate,启用它。

 

 

 

第三步

选择API和服务 -> 凭据

  1. 在 Cloud Console 中,转到创建服务帐号密钥页面。

    转到“创建服务帐号密钥”页面
  2. 从服务帐号列表中,选择新的服务帐号。
  3. 在服务帐号名称字段中,输入一个名称。
  4. 从角色列表中,选择 Project > Owner。

  5. 点击创建。包含密钥的 JSON 文件就会下载到计算机。

 

第四步

设置google app环境变量

Google api每次调用会检验凭证,需要在环境变量中设置目录到第三步所下载的Json文件

mac/linux:

$export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"

windows:

env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\my-key.json"

windows需要重启生效

 

第五步

测试API是否可以使用

本文用Nodejs测试

npm安装googletrans

npm install @google-cloud/translate

测试代码 

复制代码
 1 // Imports the Google Cloud client library
 2 const {Translate} = require('@google-cloud/translate').v2;
 3 
 4 // Instantiates a client
 5 const translate = new Translate();
 6 
 7 async function quickStart() {
 8   // The text to translate
 9   const text = 'Hello, world!';
10 
11   // The target language
12   const target = 'ru';
13 
14   // Translates some text into Russian
15   const [translation] = await translate.translate(text, target);
16   console.log(`Text: ${text}`);
17   console.log(`Translation: ${translation}`);
18 }
19 
20 quickStart();
复制代码

如果googletrans没有成功输出,则报错,检查前面的步骤


 

posted @   Asp1rant  阅读(898)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示