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

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

使用服務(wù)帳戶在 Golang 中驗(yàn)證對(duì) Cloud Function 的客戶端調(diào)用

使用服務(wù)帳戶在 Golang 中驗(yàn)證對(duì) Cloud Function 的客戶端調(diào)用

Go
守候你守候我 2022-06-01 12:27:23
我將自己的云功能部署到 GCP。在云功能上,我啟用了使用 Google 服務(wù)帳戶進(jìn)行身份驗(yàn)證。我需要編寫一個(gè) Golang 代碼來調(diào)用這個(gè)云函數(shù)。我用 Nodejs 完成了同樣的目的,但不能讓 Golang 工作。這是我的 Nodejs 代碼(工作):const {GoogleAuth} = require('google-auth-library');const targetAudience = "cloud-function-url"async function run() {    const auth = new GoogleAuth();    const client = await auth.getIdTokenClient(targetAudience);    const res = await client.request({ url });    console.info(res.data);}我的 Golang 代碼:import  "golang.org/x/oauth2/google"func getToken() (err error) {    scope := "https://www.googleapis.com/auth/cloud-platform"    client, err := google.DefaultClient(context.Background(), scope)    if err != nil {        return    }    res, err := client.Get("cloud-function-url")    if err != nil {        return    }    fmt.Println(res)    return}我也嘗試自定義要添加的代碼targetAudience,但它也不起作用baseUrl := "your-cloudfunction-baseurl"ctx := context.Background()targetAudience := baseUrlcredentials, err := google.FindDefaultCredentials(ctx)if err != nil {    fmt.Printf("cannot get credentials: %v", err)    os.Exit(1)}tokenSrc, err := google.JWTAccessTokenSourceFromJSON(credentials.JSON, targetAudience)if err != nil {    fmt.Printf("cannot create jwt source: %v", err)    os.Exit(1)}client := oauth2.NewClient(context.Background(), tokenSrc)if err != nil {    return}res, err := client.Get(baseUrl + "sub-url")if err != nil {    return}我已檢查并確保我的服務(wù)帳戶已正確加載。在上述兩種情況下,我都收到了401 "The access token could not be verified"
查看完整描述

2 回答

?
守著一只汪

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

在深入研究了Google 的 Oauth 協(xié)議OAuth2之后,我發(fā)現(xiàn) Golang 中的庫并沒有完全遵循 google 的 OAuth2 協(xié)議。

  • 谷歌規(guī)范中的流程:在 HTTP 客戶端(使用服務(wù)帳戶)中生成和簽署 JWT --> 發(fā)送到谷歌的服務(wù)器 --> 獲取新的簽名 JWT --> 將新令牌用于其他請(qǐng)求

  • Golang lib:生成并簽署 JWT --> 將此令牌用于其他請(qǐng)求

令人驚訝的是,Nodejs 庫正確處理了流程,而 Golang 庫卻沒有。我將我的調(diào)查總結(jié)為一篇博文

對(duì)于那些想要簡短回答的人,這是我的實(shí)現(xiàn)(我將一些部分移到了公共倉庫中):

import (

    "context"

    "fmt"

    "io/ioutil"

    "os"


    "github.com/CodeLinkIO/go-cloudfunction-auth/cloudfunction"


    "golang.org/x/oauth2/google"

)


func main() {

    baseUrl := "your-cloudfunction-baseurl"

    ctx := context.Background()

    targetAudience := baseUrl

    credentials, err := google.FindDefaultCredentials(ctx)

    if err != nil {

        fmt.Printf("cannot get credentials: %v", err)

        os.Exit(1)

    }


    jwtSource, err := cloudfunction.JWTAccessTokenSourceFromJSON(credentials.JSON, targetAudience)

    if err != nil {

        fmt.Printf("cannot create jwt source: %v", err)

        os.Exit(1)

    }


    client := cloudfunction.NewClient(jwtSource)

    res, err := client.Get(baseUrl + "/cloudfunction-sub-page")

    if err != nil {

        fmt.Printf("cannot fetch result: %v", err)

        os.Exit(1)

    }

    defer res.Body.Close()

    body, err := ioutil.ReadAll(res.Body)

    if err != nil {

        fmt.Printf("cannot read response: %v", err)

        os.Exit(1)

    }

    println(string(body))

}


查看完整回答
反對(duì) 回復(fù) 2022-06-01
?
呼啦一陣風(fēng)

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

我編寫了一個(gè)名為token-generator的開源應(yīng)用程序。它在 Go 中,我生成簽名身份令牌,以便能夠調(diào)用私有 Cloud Run 和 Cloud Function。

隨意使用它或復(fù)制您自己的應(yīng)用所需的核心代碼!如果您愿意,我們可以在這里討論或在 GitHub 上打開一個(gè)問題。


查看完整回答
反對(duì) 回復(fù) 2022-06-01
  • 2 回答
  • 0 關(guān)注
  • 147 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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