[英]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.