har*_*rdy 3 javascript google-api node.js google-cloud-platform
我已将此服务帐户密钥 (my-key.json) 文件存储在我的下载文件夹 (ubuntu) 中,然后将此命令运行到我的控制台中
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json"
根据 谷歌云。现在我正在运行此代码,但它引发了我的错误。
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();
Run Code Online (Sandbox Code Playgroud)
**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)
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您正在使用node <file-name>.js来初始化您的代码,您应该将命令更新为
GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" node <file-name>.js
这将使 GOOGLE_APPLICATION_CREDENTIALS 在您的节点环境中可用。
但是,作为长期解决方案,我建议创建一个.env文件并将其存储GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/my-key.json" 在该文件中。
然后按以下方式使用文件dotenv开头的包js:
require('dotenv').config();
您也可以参考以/sf/answers/1896352881/了解如何在pacakge.json.
| 归档时间: |
|
| 查看次数: |
1633 次 |
| 最近记录: |