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

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

在 Go 中驗(yàn)證 Google 登錄 ID 令牌

在 Go 中驗(yàn)證 Google 登錄 ID 令牌

Go
有只小跳蛙 2022-01-04 20:55:13
我正在尋找使用 Go 后端服務(wù)器項(xiàng)目為 Android 的 Google 登錄驗(yàn)證 ID 令牌的方法。在 Go 中使用 Google API 客戶(hù)端庫(kù)驗(yàn)證 ID 令牌的等效功能是什么?從這個(gè)頁(yè)面上使用 Google API 客戶(hù)端庫(kù)部分https://developers.google.com/identity/sign-in/android/backend-auth#using-a-google-api-client-library有 Java 和 Python 示例,還有用于使用適用于 PHP、Node.js 和其他語(yǔ)言的 Google API 客戶(hù)端庫(kù)驗(yàn)證 ID 令牌的鏈接。我檢查了我的目標(biāo)語(yǔ)言;到這里https://github.com/google/google-api-go-client/blob/master/GettingStarted.md但是,我發(fā)現(xiàn)與 Java 和 Python 示例中驗(yàn)證令牌的功能不同。Go 中有什么函數(shù)可以做這樣的事情嗎?我不想使用令牌信息端點(diǎn)https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123因?yàn)樗肓丝赡艿难舆t和網(wǎng)絡(luò)錯(cuò)誤。我希望使用 Google API 客戶(hù)端庫(kù)。請(qǐng)指導(dǎo)我應(yīng)該在哪里查看。
查看完整描述

3 回答

?
呼如林

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

這是我使用https://github.com/google/google-api-go-client庫(kù)完成的方法:


import (

    "google.golang.org/api/oauth2/v2"

    "net/http"

)


var httpClient = &http.Client{}


func verifyIdToken(idToken string) (*oauth2.Tokeninfo, error) {

    oauth2Service, err := oauth2.New(httpClient)

    tokenInfoCall := oauth2Service.Tokeninfo()

    tokenInfoCall.IdToken(idToken)

    tokenInfo, err := tokenInfoCall.Do()

    if err != nil {

        return nil, err

    }

    return tokenInfo, nil

}

oauth2.Tokeninfo 對(duì)象包含有關(guān)用戶(hù)的信息。請(qǐng)注意,這會(huì)調(diào)用https://www.googleapis.com/oauth2/v2/tokeninfo,我認(rèn)為所有 Google API 客戶(hù)端庫(kù)都會(huì)在幕后進(jìn)行此 http 調(diào)用。


查看完整回答
反對(duì) 回復(fù) 2022-01-04
?
絕地?zé)o雙

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

Google 的 idToken 實(shí)際上是 JWT 格式的,它是帶有簽名的緊湊且自包含的 JSON。


另見(jiàn):https : //jwt.io/introduction/


google-auth-library-nodejs 的 OAuth2Client.prototype.verifyIdToken 使用 Google 的公鑰驗(yàn)證 idtoken 并從 idtoken 中提取 ClaimSet,而無(wú)需調(diào)用 tokeninfo 端點(diǎn)。


我剛剛從 google-auth-library-nodejs 移植了 verifyIdToken 函數(shù),并為此創(chuàng)建了一個(gè)庫(kù):https : //github.com/futurenda/google-auth-id-token-verifier。


用法:


import (

     "github.com/futurenda/google-auth-id-token-verifier"

)


v := googleAuthIDTokenVerifier.Verifier{}

aud := "xxxxxx-yyyyyyy.apps.googleusercontent.com"

err := v.VerifyIDToken(TOKEN, []string{

    aud,

})

if err == nil {

    claimSet, err := googleAuthIDTokenVerifier.Decode(TOKEN)

    // claimSet.Iss,claimSet.Email ... (See claimset.go)

}


查看完整回答
反對(duì) 回復(fù) 2022-01-04
?
猛跑小豬

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

它非常簡(jiǎn)單,并且具有單線解決方案。只需使用官方庫(kù):


go get google.golang.org/api/idtoken"

然后編寫(xiě)以下代碼:


payload, err := idtoken.Validate(context.Background(), request.IdToken, "your google client id")

if err != nil {

    panic(err)

}

fmt.Print(payload.Claims)

然后你會(huì)得到這個(gè)輸出:


map[

    aud:<Your web application client id>

    azp:<Your android application client id>

    email:<Authenticated user email> 

    email_verified:true

    exp:<expire at>

    family_name:<Authenticated user lastname>

    given_name:<Authenticated user firstname>

    iat:<issued at>

    iss: <accounts.google.com or https://accounts.google.com>

    locale:en

    name:<Authenticated User fullname>

    picture:<Authenticated User Photo URL>

    sub: <Google Account ID [Use this to identify a id uniquely]>

]


查看完整回答
反對(duì) 回復(fù) 2022-01-04
  • 3 回答
  • 0 關(guān)注
  • 365 瀏覽
慕課專(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)