2

我正在尝试在 Google App Engine Go 中实现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)))))
}

func Call(c appengine.Context) map[string]interface{} {
    serverURL:="https://api.vaultofsatoshi.com"
    apiKey:="ENTER_YOUR_API_KEY_HERE"
    apiSecret:="ENTER_YOUR_API_SECRET_HERE"
    endpoint:="/info/order_detail"
    tr := urlfetch.Transport{Context: c}
    values := url.Values{}
    values.Set("nonce", strconv.FormatInt(time.Now().UnixNano()/1000, 10))
    signature:=GenerateSignatureFromValues(apiSecret, endpoint, values)
    req, _:=http.NewRequest("POST", serverURL+endpoint, nil)
    req.Form=values
    req.Header.Set("Api-Key", apiKey)
    req.Header.Set("Api-Sign", signature)
    resp, err:=tr.RoundTrip(req)
    if err != nil {
        c.Errorf("API post error: %s", err)
        return nil
    }
    defer resp.Body.Close()
    body, _:= ioutil.ReadAll(resp.Body)
    result := make(map[string]interface{})
    json.Unmarshal(body, &result)
    return result
}

这两段代码为相同的输入生成相同的签名。但是,当我运行 PHP 代码(使用正确的密钥和秘密)时,服务器会以正确的响应响应,但是当我运行 Go 代码时,服务器会以“无效签名”响应。这个错误表明 Go 生成的 HTTP 请求一定是格式错误的——或者 HTTP Header 的值是错误的(如果 header 值完全丢失,则会出现不同的错误),或者 POST 字段的编码方式由于某种原因是错误的。

谁能帮我找出这两段代码生成不同 HTTP 请求的原因,以及如何让 Go 生成类似于 PHP 代码的请求?

4

1 回答 1

5

请参阅 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。”

有了这条线:

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

您应该使用它而不是nil

bytes.NewBufferString(values.Encode())

另请记住,map不能保证的顺序。url.Valuesmap[string][]string。因此,您应该使用一次 Encode() 并在正文和签名中使用相同的结果。通过使用 Encode() 两次,顺序可能会有所不同。这是 Go 和 PHP 之间的一个重要区别。

您还应该养成处理error而不是忽略它的习惯。

于 2013-12-16T18:18:52.197 回答