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

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

Golang vs PHP https 調(diào)用,標頭結果不同

Golang vs PHP https 調(diào)用,標頭結果不同

Go
泛舟湖上清波郎朗 2021-06-29 13:23:23
我正在嘗試在 Google App Engine Go 中實現(xiàn)Vault of Satoshi 的 API。他們的參考 API 在 PHP 中:<?php$serverURL   = 'https://api.vaultofsatoshi.com';$apiKey      = 'ENTER_YOUR_API_KEY_HERE';$apiSecret   = 'ENTER_YOUR_API_SECRET_HERE';function usecTime() {    list($usec, $sec) = explode(' ', microtime());    $usec = substr($usec, 2, 6);    return intval($sec.$usec);}$url      = 'https://api.vaultofsatoshi.com';$endpoint = '/info/currency';$url = $serverURL . $endpoint;$parameters= array();$parameters['nonce']    = usecTime();$data = http_build_query($parameters);$httpHeaders = array(    'Api-Key: '   . $apiKey,    'Api-Sign:'   . base64_encode(hash_hmac('sha512', $endpoint . chr(0) . $data, $apiSecret)),);// Initialize the PHP curl agent$ch = curl_init();curl_setopt($ch, CURLOPT_USERAGENT, "something specific to me");curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_FAILONERROR, true);curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);$output = curl_exec($ch); curl_close($ch); echo $output;?>我的 Go 代碼如下所示:func GenerateSignatureFromValues(secretKey string, endpoint string, values url.Values) string {    query:=[]byte(values.Encode())    toEncode:=[]byte(endpoint)    toEncode = append(toEncode, 0x00)    toEncode = append(toEncode, query...)    key:=[]byte(secretKey)    hmacHash:=hmac.New(sha512.New, key)    hmacHash.Write(toEncode)    answer := hmacHash.Sum(nil)    return base64.StdEncoding.EncodeToString(([]byte(strings.ToLower(hex.EncodeToString(answer)))))}這兩段代碼為相同的輸入生成相同的簽名。但是,當我運行 PHP 代碼(使用正確的密鑰和秘密)時,服務器以正確的響應進行響應,但是當我運行 Go 代碼時,服務器以“無效簽名”響應。這個錯誤表明 Go 生成的 HTTP 請求一定是格式錯誤的 - HTTP Header 的值是錯誤的(如果頭值完全丟失,則會出現(xiàn)不同的錯誤),或者 POST 字段的編碼方式由于某種原因是錯誤的。誰能幫我找出為什么這兩段代碼生成不同的 HTTP 請求的原因,以及如何讓 Go 生成像 PHP 代碼一樣的請求?
查看完整描述

1 回答

?
當年話下

TA貢獻1890條經(jīng)驗 獲得超9個贊

請參閱 Request.Form 的文檔:


 // Form contains the parsed form data, including both the URL

 // field's query parameters and the POST or PUT form data.

 // This field is only available after ParseForm is called.

 // The HTTP client ignores Form and uses Body instead.

 Form url.Values

特別是“HTTP 客戶端忽略 Form 并使用 Body 代替?!?/p>


有了這條線:


req, _:= http.NewRequest("POST", serverURL+endpoint, nil)

您應該使用它而不是nil:


bytes.NewBufferString(values.Encode())

還要記住,map不能保證的順序。url.Values是map[string][]string。因此,您應該使用 Encode() 一次并在正文和簽名中使用相同的結果。有可能通過使用 Encode() 兩次,順序可能會有所不同。這是 Go 和 PHP 之間的一個重要區(qū)別。


你也應該養(yǎng)成處理的習慣,error而不是忽視它。


查看完整回答
反對 回復 2021-07-12
  • 1 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號