· 7 years ago · Aug 19, 2018, 09:22 PM
1//signature to acquire a session
2$apiId = '1lie8ficql9h5';
3$apiSecret = 'j6hriaKY2iZi+Y2uo9JJldmO1Bq79XB8d1v2uHzAK0Zvy972mIs8ThsJSQeDlZJz+HzmLD6Q1MUZb5X1Zf9MzQ==';
4
5//build the string to sign
6//note the order of the entries is important.
7//The http headers must be in alphabetical order by key name
8$httpMethod = 'POST';
9$apiKey = 'x-csod-api-key:'.$apiId;
10$httpUrl = '/services/api/sts/session';
11
12date_default_timezone_set('UTC');
13$date = 'x-csod-date:'.date('Y-m-d').'T'.date('H:i:s').'.000';
14$stringToSign = $httpMethod."n".$apiKey."n".$date."n".$httpUrl;
15
16/* produces the following string:
17* POSTnx-csod-api-key:1lie8ficql9h5nx-csod-date:2015-09-08T11:27:32.000n/services/api/sts/session
18*/
19
20//Generate the signature
21$secretKey = base64_decode($apiSecret);
22$signature = base64_encode(hash_hmac('sha512', $stringToSign, $secretKey, true));
23
24/*
25* signature produced:
26* 3x5ETGSoqJa4vLl8gOFzdhxReOS0k8Nk2CpKVFN2A60ItF8wfP2tr+GUY2mELXjL90B57B5imLIrzou3ZQMfqQ==
27*/
28
29<cfoutput>
30
31<cfset api_id= "1lie8ficql9h5">
32<cfset api_secret= "j6hriaKY2iZi+Y2uo9JJldmO1Bq79XB8d1v2uHzAK0Zvy972mIs8ThsJSQeDlZJz+HzmLD6Q1MUZb5X1Zf9MzQ==">
33
34<cfset api_string= "POSTnx-csod-api-key:1lie8ficql9h5nx-csod-date:2015-09-08T11:27:32.000n/services/api/sts/session">
35
36<cfset temp_key= ToString(ToBinary( api_secret))>
37
38<cfset temp_signature= HMAC( api_string, temp_key, "HMACSHA512", "UTF-8")>
39<cfset temp_signature1= ToBase64( temp_signature)>
40
41
42api_string:<br> #api_string#<br><br>
43temp_signature:<br> #temp_signature#<br>
44temp_signature1:<br> #temp_signature1#<br><br>
45
46EXPECTED: (Copied from the PHP Sample code)<br>
473x5ETGSoqJa4vLl8gOFzdhxReOS0k8Nk2CpKVFN2A60ItF8wfP2tr+GUY2mELXjL90B57B5imLIrzou3ZQMfqQ==
48</cfoutput>