我正在嘗試使用 Firebase 為我們的 Web 應用程式實作 google 登錄。這是我為獲取 idtoken 所做的作業:
const google_provider = new GoogleAuthProvider();
google_provider.addScope("openid");
google_provider.addScope("profile");
google_provider.addScope("email");
signInWithPopup(auth, google_provider).then((result) =>
result.user.getIdToken());
雖然我已經添加了組態檔范圍,但我仍然無法獲得(中的givennameandfamilyname宣告,并且是可用的)idtokenemailpicturename
(我曾嘗試直接使用谷歌登錄,在那里我可以看到所有的個人資料宣告)
我在這里錯過了什么?我做錯什么了嗎 ?
uj5u.com熱心網友回復:
回傳的 Firebase ID 令牌getIdToken()不包含您要查找的用戶的 Google 帳戶資訊(期望名稱、個人資料圖片等)。您應該使用從登錄結果中檢索到的訪問代碼向Google API發出請求以獲取它。
signInWithPopup(auth, provider)
.then(async (result) => {
// This gives you a Google Access Token. You can use it to access the Google API.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;
const response = await fetch('https://www.googleapis.com/oauth2/v1/userinfo', {
headers: {
Authorization: `Bearer ${token}`
}
})
const data = await response.json();
console.log(data);
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425743.html
