Woocommerce REST API签名不正确

时间:2017-12-07 07:56:01

标签: php wordpress woocommerce-rest-api

我目前想为Woocommerce制作一个插件,用于我试图通过API连接的一些个人内容,但我得到:

  

{ “代码”: “woocommerce_rest_authentication_error”, “消息”:“无效   签名 - 提供的签名不匹配。“,”data“:{”status“:401}}

有人知道签名无效吗?

我的卷曲请求:

$curl = curl_init();
$cnonce = hash('sha256', makeRandomString());
$requesturl = "http://my-marketplace.test/wp-json/wc/v2/products";
$timestamp = time();
$signature = createSignature($requesturl,$consumerKey, $consumerSecret, $timestamp, $cnonce);
echo $signature;
curl_setopt_array($curl, array(
  CURLOPT_URL => $requesturl,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_HTTPHEADER => array(
    "authorization: OAuth oauth_consumer_key=\"".$consumerKey."\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"".$timestamp."\",oauth_nonce=\"".$cnonce."\",oauth_signature=\"".$signature,
    "cache-control: no-cache",
    "content-type: multipart/form-data",
  ),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

我的createSignature()功能:

function createSignature($myurl, $consumerKey, $consumerSecret, $timestamp, $cnonce){
  $sig = "GET&".rawurlencode($myurl)."&";
  $sig = $sig.rawurlencode("oauth_consumer_key=".$consumerKey."&
  oauth_signature_method=HMAC-SHA1&
  oauth_timestamp=".$timestamp."&
  oauth_nonce=".$cnonce);
  return  hash_hmac ( "sha1",$sig, $consumerSecret);
}

0 个答案:

没有答案