【发布时间】:2019-12-20 08:52:00
【问题描述】:
所以我有服务帐户凭据 json(作为字符串,或我的代码中的 json 对象):
{
"type": "service_account",
"project_id": "myproject",
"private_key_id": "lkjshdjhskas",
//.... etc
}
然后我有这个简单的云存储上传 sn-p:
const {Storage} = require('@google-cloud/storage');
const creds = {service_account_object:"creds are here"};
const storage = new Storage();
const bucketName = 'my-bucket';
const filename = 'my-file.txt';
storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
cacheControl: 'public, max-age=31536000',
},
});
这当然给了我一个 invalid_grant 错误:
(node:15920) UnhandledPromiseRejectionWarning: Error: invalid_grant
at Gaxios.request (C:\Users\Yuri\Documents\Code\btests\node_modules\gaxios\build\src\gaxios.js:70:23)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:15920) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15920) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我尝试将凭据作为参数提供给Storage() 构造函数,撒谎Storage(creds) 甚至Storage(JSON.stringify(creds)),但这只会给我带来奇怪的错误。
如何使用服务帐户凭据实例化 Google 客户端库对象?
【问题讨论】:
标签: node.js google-cloud-platform google-cloud-storage google-authentication