gpt4 book ai didi

javascript - 谷歌云抛出错误 : Could not load the default credentials in Nodejs

转载 作者:行者123 更新时间:2023-12-04 01:21:35 28 4
gpt4 key购买 nike

我已将此服务帐户 key (my-key.json)文件存储在我的下载文件夹(ubuntu)中
然后我在我的控制台中运行这个命令export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"根据
google cloud .现在我正在运行此代码,但它会引发错误。

const language = require('@google-cloud/language');

const quickstart = async function () {
// Instantiates a client
const client = new language.LanguageServiceClient();

// The text to analyze
const text = 'Hello, world!';

const document = {
content: text,
type: 'PLAIN_TEXT',
};

// Detects the sentiment of the text
const [result] = await client.analyzeSentiment({document: document});
const sentiment = result.documentSentiment;

console.log(`Text: ${text}`);
console.log(`Sentiment score: ${sentiment.score}`);
console.log(`Sentiment magnitude: ${sentiment.magnitude}`);
}


quickstart();
**ERORR** -
(node:13928) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/home/hardy/Documents/personal/project/node_modules/google-auth-library/build/src/auth/googleauth.js:154:19)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at async GoogleAuth.getClient (/home/hardy/Documents/personal/project/node_modules/google-auth-library/build/src/auth/googleauth.js:485:17)
at async GrpcClient._getCredentials (/home/hardy/Documents/personal/project/node_modules/google-gax/build/src/grpc.js:88:24)
at async GrpcClient.createStub (/home/hardy/Documents/personal/project/node_modules/google-gax/build/src/grpc.js:213:23)

最佳答案

如果您使用 node <file-name>.js要初始化您的代码,您应该将命令更新为

GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" node <file-name>.js


这将使 GOOGLE_APPLICATION_CREDENTIALS 在您的 Node 环境中可用。
但是,作为一个长期解决方案,我建议创建一个 .env文件和存储 GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" 在那个文件中。
然后使用 dotenv包在您的 js 开头以下列方式归档:

require('dotenv').config();


也可以引用 https://stackoverflow.com/a/27090755/7743705了解如何在 pacakge.json 中设置环境变量.

关于javascript - 谷歌云抛出错误 : Could not load the default credentials in Nodejs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62576576/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com