[英]Querying internal metadata server from React app hosted on Google Cloud Run
我正在 Cloud Run 上托管的兩個應用程序之間實施 Google Cloud 的服務到服務身份驗證文檔。 使用 create-react-app 創建的客戶端 React 應用程序將調用服務器端 Node.js 應用程序,后者不允許未經身份驗證的調用。
但是,我無法從我的 React 應用程序查詢內部元數據服務器以獲取授權令牌。 在該頁面上的示例代碼中運行請求時,我在附加的屏幕截圖中收到錯誤。 有人對此有解決方案嗎?
撥打電話的代碼:
const buttonClickHandler = async (event) => {
const metadataServerTokenURL = 'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=';
const tokenRequestOptions = {
uri: metadataServerTokenURL + process.env.REACT_APP_API_BASE_URL,
headers: {
'Metadata-Flavor': 'Google'
}
};
// Fetch the token, then provide the token in the request to the receiving service
request(tokenRequestOptions)
.then((token) => {
return request(process.env.REACT_APP_API_BASE_URL).auth(null, null, true, token)
})
.then((response) => {
console.log(response)
})
.catch((error) => {
console.error(error)
});
}
錯誤: TypeError: Failed to fetch
正如名稱中提到的,您在問題中鏈接的這個服務到服務過程用於服務到服務的通信。
您共享的文檔提到了這一點:
請注意,此方法不適用於 GCP 之外,包括您的本地計算機。
元數據服務器不應從 Google Cloud 外部調用。 這就是為什么它在您的瀏覽器中不起作用的原因。
如果您正在調用后端容器,您可以:
firebase/auth
和 Identity Platform 實現令牌檢索。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.