Вопрос

I am currently trying out the HP cloud object storage API but all their examples are in Python and I'm working in PHP. It's saying my signature is invalid, if anyone can help see where I'm going wrong, I've attached the python example https://docs.hpcloud.com/api/object-storage/#formpost as well as my attempt in 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()

And this is my attempt...

<?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>

If anyone could provide a translation from python to PHP for the hmac_body and the signature I'm sure that would help greatly.

Это было полезно?

Решение

You forgot the newlines:

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....
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top