第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何通過(guò)google idToken獲取userInfo

如何通過(guò)google idToken獲取userInfo

Go
滄海一幻覺(jué) 2023-03-29 15:16:22
現(xiàn)在我有一個(gè) google Idtoken,我想通過(guò)令牌獲取用戶(hù)信息,從這個(gè)頁(yè)面我找到了如何驗(yàn)證和獲取 tokenInfo,在 Go 中驗(yàn)證 Google 登錄 ID 令牌 ,但 tokenInfo 不包含用戶(hù)圖片。我應(yīng)該怎么做才能獲取用戶(hù)信息?
查看完整描述

1 回答

?
不負(fù)相思意

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊

id_token 是一個(gè) jwt。我首先使用validating-google-sign-in-id-token-in-go檢查令牌是否可用。

authService, err := oauth2.New(http.DefaultClient)

if err != nil {

? ? return err

}

// check token is valid

tokenInfoCall := authService.Tokeninfo()

tokenInfoCall.IdToken(idToken)

ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Minute)

defer cancelFunc()

tokenInfoCall.Context(ctx)

tokenInfo, er := tokenInfoCall.Do()

if err != nil {

? ? // invalid token

}

然后我將 id_token 解析為 jwt,將有效負(fù)載解碼為 json。


token, _, err := new(jwt.Parser).ParseUnverified(idToken, &TokenInfo{})

if tokenInfo, ok := token.Claims.(*TokenInfo); ok {

? ? return tokenInfo, nil

} else {

? ? // parse token.payload failed

}


// TokenInfo struct

type TokenInfo struct {

? ? ? ? Iss string `json:"iss"`

? ? // userId

? ? Sub string `json:"sub"`

? ? Azp string `json:"azp"`

? ? // clientId

? ? Aud string `json:"aud"`

? ? Iat int64? `json:"iat"`

? ? // expired time

? ? Exp int64 `json:"exp"`


? ? Email? ? ? ? ?string `json:"email"`

? ? EmailVerified bool? ?`json:"email_verified"`

? ? AtHash? ? ? ? string `json:"at_hash"`

? ? Name? ? ? ? ? string `json:"name"`

? ? GivenName? ? ?string `json:"given_name"`

? ? FamilyName? ? string `json:"family_name"`

? ? Picture? ? ? ?string `json:"picture"`

? ? Local? ? ? ? ?string `json:"locale"`

? ? jwt.StandardClaims

}

價(jià)值如:


{

?// These six fields are included in all Google ID Tokens.

?"iss": "https://accounts.google.com",

?"sub": "110169484474386276334",

?"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",

?"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",

?"iat": "1433978353",

?"exp": "1433981953",


?// These seven fields are only included when the user has granted the "profile" and

?// "email" OAuth scopes to the application.

?"email": "testuser@gmail.com",

?"email_verified": "true",

?"name" : "Test User",

?"picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",

?"given_name": "Test",

?"family_name": "User",

?"locale": "en"

}

然后我得到了照片。


查看完整回答
反對(duì) 回復(fù) 2023-03-29
  • 1 回答
  • 0 關(guān)注
  • 422 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)