[英]Authenticate to google cloud as a service account on browser
我正在尝试将我的谷歌云应用程序从 Nodejs 迁移到本机 JavaScript,以便它可以在浏览器中运行。 但是,我似乎找不到任何关于如何在浏览器中作为服务帐户进行身份验证的示例。 Nodejs 中的身份验证如下所示:
const textToSpeech = require('@google-cloud/text-to-speech');
const {Storage} = require('@google-cloud/storage');
const projectId = 'project'
const keyFilename = 'key.json'
const storage = new Storage({projectId, keyFilename});
const client = new textToSpeech.TextToSpeechClient({projectId, keyFilename});
在我完成的所有搜索中,我只找到了使用 API 密钥和客户端 ID 的解决方案。 此外,所有这些解决方案都会提示用户登录,这不是我想要的。 我想做 Nodejs 代码正在做的事情,但是在本机浏览器 JavaScript 中。
google-auth-library允许从硬编码文本中加载凭据:
const {auth} = require('google-auth-library');
const {Storage} = require('@google-cloud/storage');
const authClient = auth.fromJSON({
"type": "service_account",
"project_id": "your-project-id",
"private_key_id": "your-private-key-id",
"private_key": "your-private-key",
"client_email": "your-client-email",
"client_id": "your-client-id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "your-cert-url"
});
const storage = new Storage({authClient});
但是,您应该小心。 可以提取嵌入式凭据,然后任何人都可以作为此服务帐户进行任何调用。 从理论上讲,您可以通过仔细限制访问来确保安全,但这很容易出错。
对于 GCS,您可能最好使用签名 URL或其他将凭据保存在服务器上的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.