-
-
Save idancali/4230143 to your computer and use it in GitHub Desktop.
function oauth_hmacsha1($key, $data) { | |
return base64_encode(hash_hmac('sha1', $data, $key, true)); | |
} | |
function generateHeader() | |
{ | |
$params = array (); | |
$method = "GET"; | |
$uriAndQuery = "user"; | |
$host = "api.stackmob.com"; | |
$port = "80"; | |
$timestamp = time(); | |
$nonce = substr(number_format(hexdec(sha1(microtime(true).mt_rand(10000,90000))),0,'',''), 0, 17); | |
$normalizedRequest = $timestamp. "\n" . | |
$nonce . "\n" . | |
strtoupper($method) . "\n" . | |
"/" . $uriAndQuery . "\n" . | |
$host . "\n" . | |
$port . "\n\n"; | |
echo $normalizedRequest . "<br><br>"; | |
$mac = $this->oauth_hmacsha1($macKey, $normalizedRequest); | |
$header = 'MAC id="' . $accessToken . '",ts="' . $timestamp . '",nonce="' .$nonce .'",mac="' . $mac . '"'; | |
echo $header . "<br><br>"; | |
$api = new RestClient(array( | |
'base_url' => "http://api.stackmob.com", | |
'user_agent' => 'mobileapostle', | |
'headers' => array( | |
'Accept' => 'application/vnd.stackmob+json; version=0', | |
'X-StackMob-API-Key' => $api_key, | |
'X-StackMob-User-Agent' => 'mobileapostle', | |
'Authorization' => $header | |
), | |
'format' => "")); | |
if ($method == 'GET') | |
{ | |
$result = $api->GET($uriAndQuery, $params); | |
$http_result_code = $result->info->http_code; | |
print_r($result); | |
} | |
} |
Also is the extra 'n' a typo? nonce=n
'",nonce="n' .$nonce
For reference regarding the nonce=n
- the line in the JS SDK (no 'n'):
return 'MAC id="' + id + '",ts="' + ts + '",nonce="' + nonce + '",mac="' + mac + '"';
Nevermind about the 'n', I see where it is in the JS SDK.
var nonce = "n" + Math.round(Math.random() * 10000);
so for the nonce, I just basically took your lead on that, from your gist:
var nonce = "n" + Math.round(Math.random() * 10000);
yup :)
I've updated the gist with the oauth_hmacsha1 method.
the order of the method arguments are different in PHP, that's all
I figured it out, I had to remove the 'n' from the nonce when sending the header or add it when computing the hash. The nonce I was hashing was different than the nonce I was sending over.
I updated the gist, it now works!
Thanks for your help, Erick!
Awesome!!!! Do you mind if I share this gist back in the forum?
sure, go ahead
Thanks Dan, much appreciated.
btw, saw you're in Toronto. Save me a hot dog. You guys have the best ones in the world. I used to grab some at "Mama's Best" on the UT campus when I was visiting - saw you went to UT. Hope you got to share the hot dog experience. They're amazing.
I don't know what
oauth_hmacsha1
does in particular, nor its method signature, but if it's the same as that in the JS SDK, do you have the macKey and normalizedRequest swapped? Also, do you need to base64 encode the result of$this->oauth_hmacsha1($macKey, $normalizedRequest);
vs.
May you also include your oauth_hmacsha1 method? and if it includes an external lib, do you have a link to it?
Thanks Dan!
Erick