9

I'm trying to setup some Google Maps Premier API action, and to do so, I need to sign my URLs to authenticate. If you go down to Signature examples, there is some Python, C# and Java code to show you how to do the signature via HMAC-SHA1. There is also an example so that I can to test my PHP implementation. However, I just can't seem to get it to work.

Here's my code:

$key = "vNIXE0xscrmjlyV-12Nj_BvUPaw=";

$data = "/maps/api/geocode/json?address=New+York&sensor=false&client=clientID";

$my_sign = hash_hmac("sha1", $data, base64_decode($key));
$my_sign = base64_encode($my_sign);

$valid_sign = "KrU1TzVQM7Ur0i8i7K3huiw3MsA=";

When, I run this, I get a signature of:

ZDRlNGMwZjIyMTA1MWM1Zjk0Nzc4M2NkYjlmNDQzNDBkYzk4NDI4Zg==

Which totally doesn't match.

Things I have thought about:

  1. The key is in Modified URL encoded format, so changing - and _ to + and / also doesn't work
  2. The Python example code does indeed work, so this is a valid example.
  3. Completely rewriting our code-base in python instead of PHP (I inherited it).
4

3 回答 3

21

你至少有两个问题,

  1. Google 使用特殊的 URL 安全 Base64。正常的 base64_decode 不起作用。
  2. 您需要以二进制形式生成 SHA1。

尝试这个,

$key = "vNIXE0xscrmjlyV-12Nj_BvUPaw=";
$data = "/maps/api/geocode/json?address=New+York&sensor=false&client=clientID";
$my_sign = hash_hmac("sha1", $data, base64_decode(strtr($key, '-_', '+/')), true);
$my_sign = strtr(base64_encode($my_sign), '+/', '-_');
于 2010-06-27T00:42:13.307 回答
3

一个 php 示例可在http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source

于 2012-04-26T06:43:03.967 回答
0

我假设您尝试签署 OAuth 的 url?

试试这个库:http ://code.google.com/p/oauth/

于 2010-06-26T21:09:15.273 回答