1. ホーム
  2. APIドキュメント
  3. モーションキャプチャAPIドキュメント

Sample Code / サンプルコード

Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time
import json
import hmac
import hashlib
import requests

accessKey = 'fa53be261a6f97b9dac4'
secrecKey = 'a53be261a6f97b9dac4ac4e9031afe62591e6ed8'

# /stage/user/read/getaccesskey ==> /user/read/getaccesskey
path = '/user/read/GetUserApiUsages'
method = 'POST'
body = {
"startInvokedAt": "2022-05-01T07:32:25.232Z",
"endInvokedAt": "2022-06-21T07:32:25.232Z",
"apiName": "ExtractMotionCapture",
"billedDurationSec": 0,
"billedFrame": 0,
"isError": False
}
md5enc = hashlib.md5()
# remove whitespace from json
md5enc.update(json.dumps(body).encode('utf-8'))
bodyHash = md5enc.hexdigest()
timeStr = str(int(time.time() * 1000)) # ex) '1653349880857'

h = hmac.new(bytes(secrecKey, encoding='utf-8'), digestmod=hashlib.sha256)
h.update(bytes(timeStr, encoding='utf-8'))
h.update(bytes(method, encoding='utf-8'))
h.update(bytes(path, encoding='utf-8'))
h.update(bytes(bodyHash, encoding='utf-8'))
hmacHash = h.hexdigest()

print('hmacHash: '+ hmacHash)

authHeader = 'PLSK1-HMAC-SHA256 Credential={}, Body={}, Signature={}:{}'.format(
accessKey,
bodyHash,
timeStr,
hmacHash)
print(authHeader)

baseUrl = '<https://is3yktfmf8.execute-api.ap-northeast-2.amazonaws.com/Prod>'
response = requests.post(baseUrl+path,json=body, headers={'Authorization': authHeader} )

print('response:\\n'+ json.dumps(response.json(), indent=4))

Javascript

const CryptoJS = require('crypto-js');
const request = require('request');

console.log('------------------------------------------------------------------');
const accessKey = 'fa53be261a6f97b9dac4';
const secrecKey = 'a53be261a6f97b9dac4ac4e9031afe62591e6ed8';

// /stage/user/read/getaccesskey ==> /user/read/getaccesskey
const path = '/user/read/GetUserApiUsages';
const method = 'POST';
const body = {
startInvokedAt: "2022-05-01T07:32:25.232Z",
endInvokedAt: "2022-05-21T07:32:25.232Z",
apiName: "ExtractMotionCapture",
billedDurationSec: 0,
billedFrame: 0,
isError: false
}
const bodyHash = String(CryptoJS.MD5(JSON.stringify(body))); // Note. WordArray ==> String
const time = Date.now().toString(); // ex) '1653349880857';

console.log('accessKey: ' + accessKey);
console.log('secrecKey: ' + secrecKey);
console.log('path: ' + path);
console.log('method: ' + method);
console.log('timestamp: ' + time);
console.log('bodyHash: ' + bodyHash);

var hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, secrecKey);
hmac.update(time);
hmac.update(method);
hmac.update(path);
hmac.update(bodyHash);
var hmacHash = CryptoJS.enc.Hex.stringify(hmac.finalize());
console.log('hmacHash: ' + hmacHash);

const authHeader = `PLSK1-HMAC-SHA256 Credential=${accessKey}, Body=${bodyHash}, Signature=${time}:${hmacHash}`;
console.log(authHeader);

const baseUrl = '<https://is3yktfmf8.execute-api.ap-northeast-2.amazonaws.com/Prod>';

const options = {
uri: baseUrl + path,
method: 'POST',
json: true,
body: body,
headers:{
'Authorization': authHeader
}
}
request.post(options, function(err, response, body){
console.log('err: '+ err);
//console.log('response: '+ JSON.stringify(response, null, 4));
console.log('body: '+ JSON.stringify(body,null, 4));
} );