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

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

如何:使用 golang 客戶(hù)端調(diào)用具有 IAM 授權(quán)的 API 網(wǎng)關(guān)端點(diǎn)

如何:使用 golang 客戶(hù)端調(diào)用具有 IAM 授權(quán)的 API 網(wǎng)關(guān)端點(diǎn)

看起來(lái)很簡(jiǎn)單,但我還無(wú)法弄清楚。在如下代碼中 -resp, err := http.Get("API Gateway endpoint url goes here")if err != nil {   log.Fatalln(err)}我不確定如何告訴 http.get 使用 AWS 身份驗(yàn)證。aws cli 在主機(jī)上配置了訪問(wèn)密鑰和秘密。所以我不想在代碼中再次提及。一些帖子提到了簽名請(qǐng)求。但我不確定這是否是正確的方法。鑒于 golang 中的 AWS SDK,這感覺(jué)有點(diǎn)低級(jí)。我希望AWS SDK 中必須有一種方法可以封裝簽名 HTTP 請(qǐng)求等并完成這項(xiàng)工作必須有一種方法可以通過(guò)“以某種方式”附加 aws creds 使用 http inbuild 包來(lái)執(zhí)行此操作。非常感謝任何幫助!謝謝桑迪普
查看完整描述

1 回答

?
Cats萌萌

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

一些帖子提到了簽名請(qǐng)求。但我不確定這是否是正確的方法

是的,IAM Auth 需要請(qǐng)求簽名(請(qǐng)參閱此處此處的文檔)。當(dāng)前方法是Signature v4。如果 Api Gateway 具有預(yù)期的標(biāo)頭,則它會(huì)接受請(qǐng)求?!昂灻笔翘砑诱_標(biāo)頭的過(guò)程:

# signature is derived from your secret key and the request contents.

--header 'Authorization: AWS4-HMAC-SHA256 Credential=AKIASIAXTWO8D5GSN4CS/20220712/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date, Signature=f2d8478ceff83d5cd0696502cb58a8331304846d11367d74608295c7acbfba0c'

# date prevents third parties from intercepting your request and resubmitting it later

--header 'X-Amz-Date: 20220712T124302Z'

AWS SDK 中必須有一種方法可以封裝簽名 HTTP 請(qǐng)求等并完成這項(xiàng)工作

對(duì)于多種語(yǔ)言(JS、Java 等,但不是 Go),get-sdk命令可以為您的 Rest API 生成一個(gè)SDK 客戶(hù)端,除了其他便利之外,它還包裝了簽名過(guò)程:client.privateGet(params, body, additionalParams).

必須有一種方法可以通過(guò)“以某種方式”附加 aws creds 使用 http inbuild 包來(lái)做到這一點(diǎn)

使用普通的舊 SDK,添加標(biāo)頭需要做更多的工作,可以輕松包裝為可重用的類(lèi)型:

ctx := context.TODO()


// define the request

endpoint := "https://cbi3vltq21.execute-api.us-east-1.amazonaws.com"

u, _ := url.ParseRequestURI(endpoint)

u.Path = "prod/private"

req, _ := http.NewRequest("GET", u.String(), nil)


// get the credentials from the local config files

cfg, _ := config.LoadDefaultConfig(ctx,

  config.WithRegion("us-east-1"),

  config.WithSharedConfigProfile("my-profile"))

creds, _ :=  cfg.Credentials.Retrieve(ctx) 


// hash the request body - hex value used in the signature

hash := sha256.Sum256([]byte("")) // if the request has no body, use the empty string

hexHash := fmt.Sprintf("%x", hash)


// add the Authorization and X-Amz-Date headers to the request

signer := v4.NewSigner()

_ = signer.SignHTTP(ctx, creds, req, hexHash, "execute-api", cfg.Region, time.Now())


// execute the request

client := &http.Client{}

resp, _ := client.Do(req)

H/T 這個(gè)SO answer。



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