将Google凭据关联到Vercel:NextJS 12谷歌翻译API部署报错求助
NextJS 12 + Google Cloud Translate API 部署Vercel后权限/密钥报错500
本地实现的按钮文字翻译功能运行正常,但部署到Vercel后调用翻译接口返回 'Request failed with status code 500',权限/密钥相关问题无法解决。
本地配置
将服务账户生成的key.json放在项目根目录,通过.env.local文件引用:
GOOGLE_APPLICATION_CREDENTIALS=./<projectid&key>.json
翻译端点代码(参考Google官方教程编写)
import { NextApiRequest, NextApiResponse } from "next"; import { TranslationServiceClient } from "@google-cloud/translate"; export default async (req: NextApiRequest, res: NextApiResponse) => { const translationClient = new TranslationServiceClient(); const projectId = "<myprojectID>"; const location = "global"; async function translateText() { const request = { parent: `projects/${projectId}/locations/${location}`, contents: [req.body.text], mimeType: "text/plain", sourceLanguageCode: "en", targetLanguageCode: "es", }; const [response] = await translationClient.translateText(request); res.json(response.translations[0].translatedText); } translateText(); };
已尝试的解决方法
- 在Vercel中配置单个环境变量
GOOGLE_APPLICATION_CREDENTIALS,值为整个key.json内容 - 将JSON内容转为单行格式配置
- 将密钥拆分为多个环境变量(本地也无法正常运行):
GOOGLE_ACCOUNT_TYPE=service_account GOOGLE_PROJECT_ID=project11111 GOOGLE_PRIVATE_KEY_ID=11111111111111 # 其余字段省略 - 保留
.env.local中的文件路径配置,并上传key.json文件至Vercel
以上方法均未解决问题,恳请各位提供可行的解决方案。
内容的提问来源于stack exchange,提问作者user15498557
相关产品推荐
相关产品推荐

