我目前正在尝试HP云对象存储API,但他们的所有示例都是使用Python编写的,而我正在使用PHP。 它说我的签名无效,如果有人可以帮我看看我哪里出错了,我附上了python示例https://docs.hpcloud.com/api/object-storage/#formpost以及我在PHP中的尝试。
import hmac
from hashlib import sha1
from time import time
path = '/v1/12345678912345/container/object_prefix'
redirect = 'https://myserver.com/some-page'
max_file_size = 104857600
max_file_count = 10
expires = int(time() + 600)
tenant_id = '12345678912345'
access_key_id = 'GP54NNRN2TKBVWH449AG'
secret_key = 'EHLzysK9S1QRWkwvVpVHsGZyM715OH4S2kJ'
hmac_body = '%s\n%s\n%s\n%s\n%s' % (path, redirect,
max_file_size, max_file_count, expires)
signature = tenant_id + ':' + access_key_id + ':' + hmac.new(secret_key, hmac_body, sha1).hexdigest()
这是我的尝试......
<?php
$expires = time()+600;
$hmac_body = 'https://region-a.geo-1.objects.hpcloudsvc.com/v1/xxx/'.'http://www.test.com/test.php'.'41943040'.'1'.$expires;
$signature = 'tenant_id:access_key:'.hash_hmac(sha1,$hmac_body,'secret_key', FALSE);
?>
<form action="<?php echo 'https://region-a.geo-1.objects.hpcloudsvc.com/v1/xxx/';?>" method="POST" enctype="multipart/form-data">
<input type="hidden" name="redirect" value="http://www.test.com/test.php" />
<input type="hidden" name="max_file_size" value="41943040" />
<input type="hidden" name="max_file_count" value="1" />
<input type="hidden" name="expires" value="<?php echo $expires;?>" />
<input type="hidden" name="signature" value="<?php echo $signature;?>" />
<input type="file" name="testupload" />
<input type="submit" />
</form>
如果有人可以提供从python到PHP的hmac_body和签名的翻译,我相信这会有很大的帮助。
答案 0 :(得分:0)
你忘记了换行符:
mac_body = '%s\n%s\n%s\n%s\n%s' % (path, redirect,
^^--^^--^^--^^-- newlines
$hmac_body = 'https://blahblah/xxx/'.'http://blahblah/test.php'.'41943040'.'1'.$expires;
^--here ^--here etc....