2

我正在尝试使用 PHP 中的 API v1.0(互操作模式)在 GCS 上创建一个存储桶,但我收到了“签名不匹配”错误响应。

这就是我正在做的事情:


$access_id = "GOOGxxxxxx";
$secret_key = "xyxyxyxyx/xyxyxyxyx";
$bucket = "random_bucket_name";
$url = 'https://'.$bucket.'commondatastorage.googleapis.com';
$timestamp  = date("r");

$canonicalizedResources = "/ HTTP 1.1";
$stringToSign = utf8_encode("PUT "."\n"."\n"."\n".$canonicalizedResources);
$signature  = base64_encode(hash_hmac("sha1",$stringToSign,$secret_key,true));
$authSignature = $access_id.":".$signature;

$headers = array('Host: '.$bucket.'.commondatastorage.googleapis.com',
           'Date: '.$timestamp, 'x-goog-api-version: 1', 
           'x-goog-project-id: xxxyyyxy','Content-Length: 0',
           'Authorization: GOOG1 '.$authSignature);

$c   = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c,CURLOPT_HTTPHEADER,$headers);
$xml = curl_exec($c);

这是我得到的回应:

<?xml version='1.0' encoding='UTF-8'?>
 <Error>
  <Code>SignatureDoesNotMatch</Code>
  <Message>The request signature we calculated does not match the signature you 
   provided. Check your Google secret key and signing method.</Message>
  <StringToSign>
   GET


   Sat, 03 Mar 2012 14:56:53 -0800
   x-goog-api-version:1
   x-goog-project-id:xxxyyyxy
   /random_bucket_name/
  </StringToSign>
 </Error>

有什么想法我哪里出错了吗?

这是谷歌的文档: https ://developers.google.com/storage/docs/reference-methods#putbucket

我注意到的一件事是,即使我在“stringToSign”变量中指定了“PUT”......响应说我使用了“GET”......?

任何帮助,将不胜感激。

4

1 回答 1

2

这里有几个问题:

  • 您的规范化资源应该是“/bucket/”,而不是“/HTTP 1.1”。
  • 您需要在要签名的字符串中包含两个自定义标头(x-goog-version 和 x-goog-project-id)。
  • 要签名的字符串必须包含在 Date: 标头中发送的时间戳。
  • 您需要设置 CURLOPT_PUT 以便 curl 知道发送 PUT 请求,而不是默认的 GET 请求(这就是您的错误响应暗示 GET 请求的原因)。

这是您的代码的更正版本,我对其进行了测试并用于创建新存储桶:

<?php
  $access_id = "REDACTED";
  $secret_key = "REDACTED";
  $bucket = "your-bucket";
  $url = 'https://'.$bucket.'commondatastorage.googleapis.com';
  $timestamp  = date("r");
  $version_header = "x-goog-api-version:1";
  $project_header = "x-goog-project-id:REDACTED";
  $canonicalizedResources = "/".$bucket."/";
  $stringToSign = utf8_encode("PUT\n\n\n".$timestamp."\n".$version_header."\n".$project_header."\n".$canonicalizedResources);
  $signature  = base64_encode(hash_hmac("sha1",$stringToSign,$secret_key,true));
  $authSignature = $access_id.":".$signature;

  $headers = array('Host: '.$bucket.'.commondatastorage.googleapis.com',
                   'Date: '.$timestamp, $version_header,
                   $project_header,'Content-Length: 0',
                   'Authorization: GOOG1 '.$authSignature);

  $c   = curl_init($url);
  curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($c,CURLOPT_HTTPHEADER,$headers);
  curl_setopt($c, CURLOPT_PUT, TRUE);
  $xml = curl_exec($c);
  print($xml);
?>

PS 此处提供了有关 Google Cloud Storage 的 HMAC 身份验证的所有详细信息:https ://developers.google.com/storage/docs/reference/v1/developer-guidev1#authentication

于 2012-03-04T04:37:09.287 回答