我在一個網路專案上使用 google auth,我試圖了解屬性名稱。截圖顯示了user谷歌回傳的物件。我可以通過id_token這種方式訪問:
this.user.Zb.id_token
為什么Zb?一年前是這樣的:
this.user.wc.id_token
請注意,那是wc當時,現在我的 UI 代碼中斷了。我錯過了什么?為什么使用這些屬性名稱?我如何制作它以便我可以訪問id_token它的父屬性名稱?

這是用戶界面代碼:
async authenticate(): Promise<gapi.auth2.GoogleUser> {
// Initialize gapi if not done yet
if (!this.gapiSetup) {
await this.initGoogleAuth();
}
// Resolve or reject signin Promise
return new Promise(async () => {
await this.authInstance.signIn().then(
user => {
this.user = user;
console.log('this.user: ', this.user);
this.cookieService.set('jwt', this.user.Zb.id_token, 365); // expires in one year (365 days)
// this.cookieService.set('jwt', this.user.wc.id_token, 365); // expires in one year (365 days)
this.owlerApiService.getHooterUsingIdTokenFromProvider()
.subscribe((data: any) => {
this.userDto = data;
},
error => {
console.log('error: ', error);
});
},
error => this.error = error);
});
}
async initGoogleAuth(): Promise<void> {
// Create a new Promise where the resolve function is the callback passed to gapi.load
const pload = new Promise((resolve) => {
gapi.load('auth2', resolve);
});
// When the first promise resolves, it means we have gapi loaded and that we can call gapi.init
return pload.then(async () => {
// ClientId safe to put here? Looks like it:
// https://stackoverflow.com/a/62123945/279516
await gapi.auth2
.init({ client_id: 'xxx.apps.googleusercontent.com' })
.then(auth => {
this.gapiSetup = true;
this.authInstance = auth;
});
});
}
uj5u.com熱心網友回復:
我不相信您正在以預期的形式使用 SDK;我不清楚您如何確定這是訪問id_token經過身份驗證的用戶的值的正確方法,因為官方檔案中沒有提到它。
signIn()回傳 的一個實體GoogleUser,您可以在該實體上呼叫getAuthResponse()。回傳的gapi.auth2.AuthResponse物件包括id_token:
await this.authInstance.signIn().then(
user => {
this.user = user;
console.log('this.user: ', this.user);
this.cookieService.set('jwt', this.user.getAuthResponse().id_token, 365); // expires in one year (365 days)
// …
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/348704.html
