亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 Google OpenID Connect 獲取 ID_token 的示例

如何從 Google OpenID Connect 獲取 ID_token 的示例

Go
暮色呼如 2023-06-19 13:53:28
我正在嘗試從 OpenID 獲取用戶信息https://developers.google.com/identity/protocols/OpenIDConnect#exchangecode我從以下方面得到了象征性的回應:tok, err := bookshelf.OAuthConfig.Exchange(context.Background(), code)我可以檢查tok并看到有一個id_token元素:log.Printf("************ tok  contains %#v", tok)2019/05/13 15:11:24 ************ tok  contains &oauth2.Token{AccessToken:"XXXXXXXXXXX", TokenType:"Bearer", RefreshToken:"", Expiry:time.Time{wall:0xbf2f2c6b181f7b0f, ext:3608126524112, loc:(*time.Location)(0x18516e0)}, raw:map[string]interface {}{"scope":"openid https://www.googleapis.com/auth/profile.emails.read", "token_type":"Bearer", "id_token":"Base64-encodedJSONobject", "access_token":"HASH", "expires_in":3600}}我不明白如何從id_token. 在哪里可以找到從 JWT 獲取負載的示例id_token?當我試圖解碼id_tokenid_token := base64.StdEncoding.DecodeString(tok.id_token)我收到這些錯誤:multiple-value base64.StdEncoding.DecodeString() in single-value context/var/folders/mw/0y88j8_54bjc93d_lg3120qw0000gp/T/tmpbPyjTZappengine-go-bin/auth.go:133:49: tok.id_token undefined (type *oauth2.Token has no field or method id_token)如何使用 OpenID 而不是 Google+ 實現與下面的fetchProfile函數等效的功能?我能找到的唯一示例使用已棄用的 Google+ 庫:func oauthCallbackHandler(w http.ResponseWriter, r *http.Request) *appError {    oauthFlowSession, err := bookshelf.SessionStore.Get(r, r.FormValue("state"))    if err != nil {        return appErrorf(err, "invalid state parameter. try logging in again.")    }    redirectURL, ok := oauthFlowSession.Values[oauthFlowRedirectKey].(string)    // Validate this callback request came from the app.    if !ok {        return appErrorf(err, "invalid state parameter. try logging in again.")    }    code := r.FormValue("code")    tok, err := bookshelf.OAuthConfig.Exchange(context.Background(), code)    if err != nil {        return appErrorf(err, "could not get auth token: %v", err)    }Golang 文檔中有多少漏洞令人驚訝。
查看完整描述

2 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

嘗試這個:


type jwtClaims struct {

    Iss string `json:"iss"`

    Azp string `json:"azp"`

    Aud string `json:"aud"`

    Sub string `json:"sub"`

    Hd string `json:"hd"`

    Email string `json:"email"`

    EmailVerified bool `json:"email_verified"`

    AtHash  string `json:"at_hash"`

    Nonce string `json:"nonce"`

    Iat int `json:"iat"`

    Exp int `json:"exp"`

}


func extractJwtClaims(token string) (*jwtClaims, error) {

    tokenStruct := &jwtClaims{}

    jwtParts := strings.Split(token, ".")

    out, _ := base64.RawURLEncoding.DecodeString(jwtParts[1])

    err := json.Unmarshal(out, &tokenStruct)

    if err != nil {

        return nil, err

    }


    return tokenStruct, nil

}


func main() {

    token, _ := extractJwtClaims(tok.id_token)

}


查看完整回答
反對 回復 2023-06-19
?
夢里花落0921

TA貢獻1772條經驗 獲得超6個贊

我得到了“id_token”,就像下面的代碼一樣。


import "golang.org/x/oauth2"


func GetIDTokenWithTokenInfo(tokenInfo *oauth2.Token) string {

    rawIDToken := tokenInfo.Extra("id_token").(string)

    return rawIDToken

}


查看完整回答
反對 回復 2023-06-19
  • 2 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號