我希望在我的網站上獲取一個已登錄的用戶(不是我)的Firebase專案的串列。
我似乎無法正確地請求 firebase rest api(endpoint),因為我收到一個 401 UNAUTHENTICATED 錯誤。
我所遵循的步驟:firebase.auth().signInWithPopup來驗證用戶。以下是所使用的作用域:
// Google provider。
const provider = new GoogleAuthProvider();
//增加范圍
provider.addScope("https://www.googleapis.com/auth/cloud-platform"/span>)。
provider.addScope("https://www.googleapis.com/auth/firebase") 。
provider.addScope("https://www.googleapis.com/auth/datastore") 。
//登錄
signInWithPopup(auth, provider)。
然后我使用getIdToken方法來從用戶那里獲得一個令牌。
const token = await auth.currentUser.getIdToken(true);
我使用這個token來請求https://firebase.googleapis.com/v1beta1/projects:
const data = await window. fetch("https://firebase.googleapis.com/v1beta1/projects"/span>, {
headers: {
authorization: `Bearer ${token}`。
},
})
.then(res => res.json() )。
但是我總是得到一個401的錯誤:
。{
"error"/span>: {
"code": 401,
"message": "Request had invalid authentication credentials. 期待OAuth 2訪問令牌、登錄cookie或其他有效的認證憑證。見https://developers.google.com/identity/sign-in/web/devconsole-project。"。
"status"。"UNAUTHENTICATED"。
}
我做錯了什么?
uj5u.com熱心網友回復:
為了獲得一個用戶的Firebase專案串列,你需要一個作為專案合作者的用戶的OAuth2令牌。你試圖使用一個Firebase認證用戶的ID令牌/JWT,這是不一樣的。
按照生成訪問令牌的檔案來獲取你可以用來獲取專案串列的值。
uj5u.com熱心網友回復:
我剛剛找到了答案,firebase SDK確實給了你一個帶有idToken的accessToken。你可以用這個令牌代替idToken來滿足你的任何REST api需求。
const result = await signInWithPopup(auth, provider) 。
//這將給你一個谷歌訪問令牌。你可以用它來訪問Google API。
const credential = GoogleAuthProvider.credentialFromResult(result)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/325767.html
標籤:
上一篇:小于或等于x的素數的數量
