iink SDK Web

Answered

Issues using the Batch Export REST API directly without iinkJS

Hi there, 


I am trying to directly send requests to the REST API at http://cloud.myscript.com/api/v4.0/iink/batch and although there is a SwaggerUI at https://swaggerui.myscript.com I run into issues and have a bit of hard time figuring out why. I put together a NodeJS script that computes the hmac and puts together a request:


 

const crypto = require("crypto");
const fetch = (...args) =>
  import("node-fetch").then(({ default: fetch }) => fetch(...args));

const applicationKey = "xyz";
const hmacKey = "abc";

const payload = {
  configuration: {
    text: {
      guides: { enable: true },
      smartGuide: true,
      smartGuideFadeOut: { enable: false, duration: 10000 },
      mimeTypes: ["text/plain", "application/vnd.myscript.jiix"],
      margin: { top: 20, left: 10, right: 10 },
    },
    lang: "en_US",
    export: {
      "image-resolution": 300,
      jiix: {
        "bounding-box": false,
        strokes: false,
        text: { chars: false, words: true },
      },
    },
  },
  xDPI: 96,
  yDPI: 96,
  contentType: "Text",
  theme:
    "ink {\ncolor: #000000;\n-myscript-pen-width: 1;\n-myscript-pen-fill-style: none;\n-myscript-pen-fill-color: #FFFFFF00;\n}\n.math {\nfont-family: STIXGeneral;\n}\n.math-solved {\nfont-family: STIXGeneral;\ncolor: #A8A8A8FF;\n}\n.text {\nfont-family: Open Sans;\nfont-size: 10;\n}\n",
  strokeGroups: [
    {
      penStyle: null,
      strokes: [
        {
          x: [
            151, 151, 151, 151, 150, 150, 150, 149, 149, 148, 147, 147, 146,
            146,
          ],
          y: [26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65],
          t: [
            1654677909781, 1654677909818, 1654677909835, 1654677909848,
            1654677909859, 1654677909868, 1654677909876, 1654677909885,
            1654677909894, 1654677909903, 1654677909913, 1654677909922,
            1654677909932, 1654677909943,
          ],
        },
        {
          x: [166, 166, 166, 165, 165, 165, 164, 164, 163, 163, 162, 161],
          y: [30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63],
          t: [
            1654677910212, 1654677910236, 1654677910249, 1654677910258,
            1654677910266, 1654677910275, 1654677910283, 1654677910292,
            1654677910301, 1654677910309, 1654677910320, 1654677910335,
          ],
        },
        {
          x: [144, 147, 150, 153, 156, 159, 162, 165],
          y: [45, 45, 45, 45, 45, 45, 46, 46],
          t: [
            1654677910586, 1654677910636, 1654677910648, 1654677910655,
            1654677910665, 1654677910673, 1654677910682, 1654677910694,
          ],
        },
        {
          x: [
            169, 172, 175, 178, 181, 184, 184, 182, 179, 176, 173, 170, 167,
            165, 164, 164, 166, 169, 172, 175, 178, 181, 184, 187, 190, 193,
            196, 199, 202, 205, 208, 210, 212, 213, 213, 213, 213, 213, 211,
            208, 205, 202, 199, 196, 193, 190, 188, 186, 184, 183, 182, 182,
            182, 183, 186, 189, 192, 195, 198, 201, 204, 207, 210, 213, 216,
            219, 222, 225, 228, 231, 233, 236, 238, 239, 240, 240, 241, 241,
            240, 237, 234, 231, 228, 225, 222, 219, 216, 213, 211, 210, 209,
            209, 209, 209, 209, 211, 212, 215, 217, 220, 223, 226, 229, 232,
            233,
          ],
          y: [
            46, 47, 47, 47, 47, 47, 44, 41, 40, 39, 39, 39, 39, 42, 45, 48, 51,
            53, 55, 56, 57, 57, 58, 58, 58, 58, 57, 56, 56, 54, 52, 49, 46, 43,
            40, 37, 34, 31, 28, 26, 25, 24, 24, 24, 25, 27, 30, 33, 36, 39, 42,
            45, 48, 51, 54, 56, 58, 58, 59, 59, 59, 59, 59, 59, 59, 58, 58, 56,
            55, 53, 50, 48, 45, 42, 39, 36, 33, 30, 27, 25, 23, 22, 22, 22, 22,
            23, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 57, 60, 62, 64, 65,
            65, 65, 62,
          ],
          t: [
            1654677910923, 1654677910940, 1654677910956, 1654677910970,
            1654677910984, 1654677911009, 1654677911034, 1654677911058,
            1654677911070, 1654677911082, 1654677911098, 1654677911110,
            1654677911126, 1654677911145, 1654677911167, 1654677911189,
            1654677911206, 1654677911221, 1654677911236, 1654677911247,
            1654677911258, 1654677911268, 1654677911273, 1654677911278,
            1654677911284, 1654677911290, 1654677911298, 1654677911303,
            1654677911309, 1654677911316, 1654677911325, 1654677911332,
            1654677911341, 1654677911348, 1654677911355, 1654677911361,
            1654677911368, 1654677911377, 1654677911388, 1654677911401,
            1654677911414, 1654677911426, 1654677911437, 1654677911448,
            1654677911458, 1654677911472, 1654677911483, 1654677911494,
            1654677911506, 1654677911516, 1654677911526, 1654677911535,
            1654677911544, 1654677911552, 1654677911564, 1654677911573,
            1654677911582, 1654677911589, 1654677911597, 1654677911603,
            1654677911609, 1654677911615, 1654677911620, 1654677911626,
            1654677911632, 1654677911637, 1654677911643, 1654677911650,
            1654677911656, 1654677911664, 1654677911671, 1654677911678,
            1654677911685, 1654677911691, 1654677911697, 1654677911704,
            1654677911714, 1654677911723, 1654677911735, 1654677911747,
            1654677911756, 1654677911766, 1654677911775, 1654677911783,
            1654677911792, 1654677911803, 1654677911815, 1654677911824,
            1654677911835, 1654677911844, 1654677911852, 1654677911859,
            1654677911866, 1654677911873, 1654677911881, 1654677911889,
            1654677911898, 1654677911906, 1654677911914, 1654677911925,
            1654677911935, 1654677911945, 1654677911957, 1654677911989,
            1654677912015,
          ],
        },
        {
          x: [
            244, 241, 238, 237, 236, 235, 235, 235, 238, 241, 244, 247, 250,
            251, 252, 252, 252, 249, 246, 243, 240, 237, 234,
          ],
          y: [
            44, 44, 46, 49, 52, 55, 58, 61, 63, 63, 64, 63, 60, 57, 54, 51, 48,
            45, 44, 43, 43, 43, 44,
          ],
          t: [
            1654677912196, 1654677912213, 1654677912231, 1654677912241,
            1654677912254, 1654677912266, 1654677912278, 1654677912292,
            1654677912311, 1654677912328, 1654677912341, 1654677912353,
            1654677912370, 1654677912381, 1654677912390, 1654677912401,
            1654677912415, 1654677912430, 1654677912441, 1654677912449,
            1654677912457, 1654677912470, 1654677912498,
          ],
        },
      ],
    },
  ],
  height: 0,
  width: 0,
  conversionState: "DIGITAL_EDIT",
};

const userKey = applicationKey + hmacKey;

const algorithm = "sha512";
const encoding = "base64";

const hmac = crypto
  .createHmac(algorithm, userKey)
  .update(Buffer.from(JSON.stringify(payload)).toString(encoding))
  .digest("hex")
  .toUpperCase();

console.log(`hmac => ${hmac}`);

async function start() {
  const response = await fetch(
    "http://cloud.myscript.com/api/v4.0/iink/batch",
    {
      body: JSON.stringify(payload),
      headers: {
        accept: "application/json,application/vnd.myscript.jiix",
        applicationKey: applicationKey,
        "Content-Type": "application/json",
        hmac: hmac,
      },
      method: "POST",
    }
  );

  const data = await response.json();

  console.log(`data => ${JSON.stringify(data)}`);
}

start();

Of course I am using the proper applicationKey / hmacKey. However, with HMAC disabled in the dashboard I get:

data => {"timestamp":1654678862088,"status":405,"error":"Method Not Allowed","path":"/api/v4.0/iink/batch"}


With HMAC enabled in the dashboard I get: 

data => {"code":"access.not.granted","message":"Access not granted"}


Any ideas?


Best Answer

Hi Olivier,


you were right. I went to the dashboard in the MyScript Cloud under applications and tested one of the keys with protocol set to REST and then took one of the tests requests using the Chrome Dev Tools network tab. Then I was able to compare a request that works against mine and it turns out the payload simply had newlines (\n) characters that were supposed to be escaped in the payload (\\n). I ended up using JSON.stringify(payload).replace(/\n/g, "\\n") instead of just JSON.stringify(payload). Here the code for anyone struggling with the same issue:

const CryptoJS = require("crypto-js");
const fetch = (...args) =>
  import("node-fetch").then(({ default: fetch }) => fetch(...args));

const applicationKey = "abc";
const hmacKey = "xyz";

const json = {
  configuration: {
    text: {
      guides: { enable: true },
      smartGuide: true,
      smartGuideFadeOut: { enable: false, duration: 10000 },
      mimeTypes: ["text/plain", "application/vnd.myscript.jiix"],
      margin: { top: 20, left: 10, right: 10 },
    },
    lang: "en_US",
  },
  xDPI: 96,
  yDPI: 96,
  contentType: "Text",
  theme:
    "ink {\ncolor: #000000;\n-myscript-pen-width: 1;\n-myscript-pen-fill-style: none;\n-myscript-pen-fill-color: #FFFFFF00;\n}\n.math {\nfont-family: STIXGeneral;\n}\n.math-solved {\nfont-family: STIXGeneral;\ncolor: #A8A8A8FF;\n}\n.text {\nfont-family: Open Sans;\nfont-size: 10;\n}\n",
  strokeGroups: [
    {
      penStyle: null,
      strokes: [
        {
          x: [
            151, 151, 151, 151, 150, 150, 150, 149, 149, 148, 147, 147, 146,
            146,
          ],
          y: [26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65],
          t: [
            1654677909781, 1654677909818, 1654677909835, 1654677909848,
            1654677909859, 1654677909868, 1654677909876, 1654677909885,
            1654677909894, 1654677909903, 1654677909913, 1654677909922,
            1654677909932, 1654677909943,
          ],
        },
        {
          x: [166, 166, 166, 165, 165, 165, 164, 164, 163, 163, 162, 161],
          y: [30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63],
          t: [
            1654677910212, 1654677910236, 1654677910249, 1654677910258,
            1654677910266, 1654677910275, 1654677910283, 1654677910292,
            1654677910301, 1654677910309, 1654677910320, 1654677910335,
          ],
        },
        {
          x: [144, 147, 150, 153, 156, 159, 162, 165],
          y: [45, 45, 45, 45, 45, 45, 46, 46],
          t: [
            1654677910586, 1654677910636, 1654677910648, 1654677910655,
            1654677910665, 1654677910673, 1654677910682, 1654677910694,
          ],
        },
        {
          x: [
            169, 172, 175, 178, 181, 184, 184, 182, 179, 176, 173, 170, 167,
            165, 164, 164, 166, 169, 172, 175, 178, 181, 184, 187, 190, 193,
            196, 199, 202, 205, 208, 210, 212, 213, 213, 213, 213, 213, 211,
            208, 205, 202, 199, 196, 193, 190, 188, 186, 184, 183, 182, 182,
            182, 183, 186, 189, 192, 195, 198, 201, 204, 207, 210, 213, 216,
            219, 222, 225, 228, 231, 233, 236, 238, 239, 240, 240, 241, 241,
            240, 237, 234, 231, 228, 225, 222, 219, 216, 213, 211, 210, 209,
            209, 209, 209, 209, 211, 212, 215, 217, 220, 223, 226, 229, 232,
            233,
          ],
          y: [
            46, 47, 47, 47, 47, 47, 44, 41, 40, 39, 39, 39, 39, 42, 45, 48, 51,
            53, 55, 56, 57, 57, 58, 58, 58, 58, 57, 56, 56, 54, 52, 49, 46, 43,
            40, 37, 34, 31, 28, 26, 25, 24, 24, 24, 25, 27, 30, 33, 36, 39, 42,
            45, 48, 51, 54, 56, 58, 58, 59, 59, 59, 59, 59, 59, 59, 58, 58, 56,
            55, 53, 50, 48, 45, 42, 39, 36, 33, 30, 27, 25, 23, 22, 22, 22, 22,
            23, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 57, 60, 62, 64, 65,
            65, 65, 62,
          ],
          t: [
            1654677910923, 1654677910940, 1654677910956, 1654677910970,
            1654677910984, 1654677911009, 1654677911034, 1654677911058,
            1654677911070, 1654677911082, 1654677911098, 1654677911110,
            1654677911126, 1654677911145, 1654677911167, 1654677911189,
            1654677911206, 1654677911221, 1654677911236, 1654677911247,
            1654677911258, 1654677911268, 1654677911273, 1654677911278,
            1654677911284, 1654677911290, 1654677911298, 1654677911303,
            1654677911309, 1654677911316, 1654677911325, 1654677911332,
            1654677911341, 1654677911348, 1654677911355, 1654677911361,
            1654677911368, 1654677911377, 1654677911388, 1654677911401,
            1654677911414, 1654677911426, 1654677911437, 1654677911448,
            1654677911458, 1654677911472, 1654677911483, 1654677911494,
            1654677911506, 1654677911516, 1654677911526, 1654677911535,
            1654677911544, 1654677911552, 1654677911564, 1654677911573,
            1654677911582, 1654677911589, 1654677911597, 1654677911603,
            1654677911609, 1654677911615, 1654677911620, 1654677911626,
            1654677911632, 1654677911637, 1654677911643, 1654677911650,
            1654677911656, 1654677911664, 1654677911671, 1654677911678,
            1654677911685, 1654677911691, 1654677911697, 1654677911704,
            1654677911714, 1654677911723, 1654677911735, 1654677911747,
            1654677911756, 1654677911766, 1654677911775, 1654677911783,
            1654677911792, 1654677911803, 1654677911815, 1654677911824,
            1654677911835, 1654677911844, 1654677911852, 1654677911859,
            1654677911866, 1654677911873, 1654677911881, 1654677911889,
            1654677911898, 1654677911906, 1654677911914, 1654677911925,
            1654677911935, 1654677911945, 1654677911957, 1654677911989,
            1654677912015,
          ],
        },
        {
          x: [
            244, 241, 238, 237, 236, 235, 235, 235, 238, 241, 244, 247, 250,
            251, 252, 252, 252, 249, 246, 243, 240, 237, 234,
          ],
          y: [
            44, 44, 46, 49, 52, 55, 58, 61, 63, 63, 64, 63, 60, 57, 54, 51, 48,
            45, 44, 43, 43, 43, 44,
          ],
          t: [
            1654677912196, 1654677912213, 1654677912231, 1654677912241,
            1654677912254, 1654677912266, 1654677912278, 1654677912292,
            1654677912311, 1654677912328, 1654677912341, 1654677912353,
            1654677912370, 1654677912381, 1654677912390, 1654677912401,
            1654677912415, 1654677912430, 1654677912441, 1654677912449,
            1654677912457, 1654677912470, 1654677912498,
          ],
        },
      ],
    },
  ],
  height: 0,
  width: 0,
  conversionState: "DIGITAL_EDIT",
};

const body = JSON.stringify(json).replace(/\n/g, "\\n");

const hmac = new CryptoJS.HmacSHA512(body, applicationKey + hmacKey).toString(
  CryptoJS.Hex
);

async function start() {
  const response = await fetch(
    "https://cloud.myscript.com/api/v4.0/iink/batch",
    {
      headers: {
        accept: "application/json,application/vnd.myscript.jiix", // text/plain for just text
        applicationkey: applicationKey,
        "content-type": "application/json",
        hmac: hmac,
      },
      body: body,
      method: "POST",
    }
  );

  const res = await response.json(); // response.text() in case of text/plain

  console.log(`res => ${JSON.stringify(res)}`);
}

start();

 Thanks Olivier! 


Dear Brhempen,

thank you for contacting us.

Currently, there are basically 2 issues in your code:
-For the first error "error":"Method Not Allowed, it is likely your payload is not properly formatted.
Simply taking your payload, and removing the commas at the end of each "square bracket" field, I could post the request:
Please find the payload I posted:


{"configuration": {
"text": {
"guides": {"enable":true},"smartGuide":true,"smartGuideFadeOut":{"enable":false,"duration":10000},
"mimeTypes": ["text/plain", "application/vnd.myscript.jiix"],
"margin": {"top": 20,"left": 10,"right": 10}
},
"lang": "en_US"
},
"xDPI": 96,
"yDPI": 96,
"contentType": "Text",
"theme":
"ink {\ncolor: #000000;\n-myscript-pen-width: 1;\n-myscript-pen-fill-style: none;\n-myscript-pen-fill-color: #FFFFFF00;\n}\n.math {\nfont-family: STIXGeneral;\n}\n.math-solved {\nfont-family: STIXGeneral;\ncolor: #A8A8A8FF;\n}\n.text {\nfont-family: Open Sans;\nfont-size: 10;\n}\n",
"strokeGroups": [
{
"penStyle": null,
"strokes": [
{
"x": [
151, 151, 151, 151, 150, 150, 150, 149, 149, 148, 147, 147, 146, 146
],
"y": [26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65],
"t": [
1654677909781, 1654677909818, 1654677909835, 1654677909848,
1654677909859, 1654677909868, 1654677909876, 1654677909885,
1654677909894, 1654677909903, 1654677909913, 1654677909922,
1654677909932, 1654677909943
]
}
...
]
}
],
"height":0,"width":0,"conversionState":"DIGITAL_EDIT"}


-Regarding the Access not granted error, the reason is likely the compute hmac is not done properly. Here is the function we use in the iinkJS (see the iinkJS\src\recognizer\CryptoHelper.js file):


export function computeHmac (input, applicationKey, hmacKey) {
const jsonInput = (typeof input === 'object') ? JSON.stringify(input) : input
logger.debug('The HmacSHA512 function is loaded', HmacSHA512)
return new HmacSHA512(jsonInput, applicationKey + hmacKey).toString(Hex)


Best regards,

Olivier

Thanks for the quick reply Oliver!

- To your first suggestion about the payload: It seems that JSON.stringify(payload) automatically removes commas at the end of each square bracket field in the payload automatically when converting to a string. 

- I am now using the hmac calculation from iinkJS just like suggested by you but still no luck.

Here's the slightly modified code:

 

const CryptoJS = require("crypto-js");
const fetch = (...args) =>
  import("node-fetch").then(({ default: fetch }) => fetch(...args));

const applicationKey = "abc";
const hmacKey = "xyz";

const payload = {
  configuration: {
    text: {
      guides: { enable: true },
      smartGuide: true,
      smartGuideFadeOut: { enable: false, duration: 10000 },
      mimeTypes: ["text/plain", "application/vnd.myscript.jiix"],
      margin: { top: 20, left: 10, right: 10 },
    },
    lang: "en_US",
    export: {
      "image-resolution": 300,
      jiix: {
        "bounding-box": false,
        strokes: false,
        text: { chars: false, words: true },
      },
    },
  },
  xDPI: 96,
  yDPI: 96,
  contentType: "Text",
  theme:
    "ink {\ncolor: #000000;\n-myscript-pen-width: 1;\n-myscript-pen-fill-style: none;\n-myscript-pen-fill-color: #FFFFFF00;\n}\n.math {\nfont-family: STIXGeneral;\n}\n.math-solved {\nfont-family: STIXGeneral;\ncolor: #A8A8A8FF;\n}\n.text {\nfont-family: Open Sans;\nfont-size: 10;\n}\n",
  strokeGroups: [
    {
      penStyle: null,
      strokes: [
        {
          x: [
            151, 151, 151, 151, 150, 150, 150, 149, 149, 148, 147, 147, 146,
            146,
          ],
          y: [26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65],
          t: [
            1654677909781, 1654677909818, 1654677909835, 1654677909848,
            1654677909859, 1654677909868, 1654677909876, 1654677909885,
            1654677909894, 1654677909903, 1654677909913, 1654677909922,
            1654677909932, 1654677909943,
          ],
        },
        {
          x: [166, 166, 166, 165, 165, 165, 164, 164, 163, 163, 162, 161],
          y: [30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63],
          t: [
            1654677910212, 1654677910236, 1654677910249, 1654677910258,
            1654677910266, 1654677910275, 1654677910283, 1654677910292,
            1654677910301, 1654677910309, 1654677910320, 1654677910335,
          ],
        },
        {
          x: [144, 147, 150, 153, 156, 159, 162, 165],
          y: [45, 45, 45, 45, 45, 45, 46, 46],
          t: [
            1654677910586, 1654677910636, 1654677910648, 1654677910655,
            1654677910665, 1654677910673, 1654677910682, 1654677910694,
          ],
        },
        {
          x: [
            169, 172, 175, 178, 181, 184, 184, 182, 179, 176, 173, 170, 167,
            165, 164, 164, 166, 169, 172, 175, 178, 181, 184, 187, 190, 193,
            196, 199, 202, 205, 208, 210, 212, 213, 213, 213, 213, 213, 211,
            208, 205, 202, 199, 196, 193, 190, 188, 186, 184, 183, 182, 182,
            182, 183, 186, 189, 192, 195, 198, 201, 204, 207, 210, 213, 216,
            219, 222, 225, 228, 231, 233, 236, 238, 239, 240, 240, 241, 241,
            240, 237, 234, 231, 228, 225, 222, 219, 216, 213, 211, 210, 209,
            209, 209, 209, 209, 211, 212, 215, 217, 220, 223, 226, 229, 232,
            233,
          ],
          y: [
            46, 47, 47, 47, 47, 47, 44, 41, 40, 39, 39, 39, 39, 42, 45, 48, 51,
            53, 55, 56, 57, 57, 58, 58, 58, 58, 57, 56, 56, 54, 52, 49, 46, 43,
            40, 37, 34, 31, 28, 26, 25, 24, 24, 24, 25, 27, 30, 33, 36, 39, 42,
            45, 48, 51, 54, 56, 58, 58, 59, 59, 59, 59, 59, 59, 59, 58, 58, 56,
            55, 53, 50, 48, 45, 42, 39, 36, 33, 30, 27, 25, 23, 22, 22, 22, 22,
            23, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 57, 60, 62, 64, 65,
            65, 65, 62,
          ],
          t: [
            1654677910923, 1654677910940, 1654677910956, 1654677910970,
            1654677910984, 1654677911009, 1654677911034, 1654677911058,
            1654677911070, 1654677911082, 1654677911098, 1654677911110,
            1654677911126, 1654677911145, 1654677911167, 1654677911189,
            1654677911206, 1654677911221, 1654677911236, 1654677911247,
            1654677911258, 1654677911268, 1654677911273, 1654677911278,
            1654677911284, 1654677911290, 1654677911298, 1654677911303,
            1654677911309, 1654677911316, 1654677911325, 1654677911332,
            1654677911341, 1654677911348, 1654677911355, 1654677911361,
            1654677911368, 1654677911377, 1654677911388, 1654677911401,
            1654677911414, 1654677911426, 1654677911437, 1654677911448,
            1654677911458, 1654677911472, 1654677911483, 1654677911494,
            1654677911506, 1654677911516, 1654677911526, 1654677911535,
            1654677911544, 1654677911552, 1654677911564, 1654677911573,
            1654677911582, 1654677911589, 1654677911597, 1654677911603,
            1654677911609, 1654677911615, 1654677911620, 1654677911626,
            1654677911632, 1654677911637, 1654677911643, 1654677911650,
            1654677911656, 1654677911664, 1654677911671, 1654677911678,
            1654677911685, 1654677911691, 1654677911697, 1654677911704,
            1654677911714, 1654677911723, 1654677911735, 1654677911747,
            1654677911756, 1654677911766, 1654677911775, 1654677911783,
            1654677911792, 1654677911803, 1654677911815, 1654677911824,
            1654677911835, 1654677911844, 1654677911852, 1654677911859,
            1654677911866, 1654677911873, 1654677911881, 1654677911889,
            1654677911898, 1654677911906, 1654677911914, 1654677911925,
            1654677911935, 1654677911945, 1654677911957, 1654677911989,
            1654677912015,
          ],
        },
        {
          x: [
            244, 241, 238, 237, 236, 235, 235, 235, 238, 241, 244, 247, 250,
            251, 252, 252, 252, 249, 246, 243, 240, 237, 234,
          ],
          y: [
            44, 44, 46, 49, 52, 55, 58, 61, 63, 63, 64, 63, 60, 57, 54, 51, 48,
            45, 44, 43, 43, 43, 44,
          ],
          t: [
            1654677912196, 1654677912213, 1654677912231, 1654677912241,
            1654677912254, 1654677912266, 1654677912278, 1654677912292,
            1654677912311, 1654677912328, 1654677912341, 1654677912353,
            1654677912370, 1654677912381, 1654677912390, 1654677912401,
            1654677912415, 1654677912430, 1654677912441, 1654677912449,
            1654677912457, 1654677912470, 1654677912498,
          ],
        },
      ],
    },
  ],
  height: 0,
  width: 0,
  conversionState: "DIGITAL_EDIT",
};

const jsonInput = typeof input === "object" ? JSON.stringify(payload) : payload;
console.log("The HmacSHA512 function is loaded", CryptoJS.HmacSHA512);

const hmac = new CryptoJS.HmacSHA512(
  jsonInput,
  applicationKey + hmacKey
).toString(CryptoJS.Hex);

async function start() {
  const response = await fetch(
    "http://cloud.myscript.com/api/v4.0/iink/batch",
    {
      body: JSON.stringify(payload),
      headers: {
        accept: "application/json,application/vnd.myscript.jiix",
        applicationKey: applicationKey,
        "Content-Type": "application/json",
        hmac: hmac,
      },
      method: "POST",
    }
  );

  const data = await response.json();

  console.log(`data => ${JSON.stringify(data)}`);
}

start();

Reponse still is data => {"timestamp":1654702964552,"status":405,"error":"Method Not Allowed","path":"/api/v4.0/iink/batch"}

Any more suggestions? 


Dear Brhempen,


currently, can you please check the payload that is posted on our server and attach it? The idea is simply to understand what is wrong in the latter? It is likely your code is mis-formatting the payload.


Best regards,


Olivier


Answer

Hi Olivier,


you were right. I went to the dashboard in the MyScript Cloud under applications and tested one of the keys with protocol set to REST and then took one of the tests requests using the Chrome Dev Tools network tab. Then I was able to compare a request that works against mine and it turns out the payload simply had newlines (\n) characters that were supposed to be escaped in the payload (\\n). I ended up using JSON.stringify(payload).replace(/\n/g, "\\n") instead of just JSON.stringify(payload). Here the code for anyone struggling with the same issue:

const CryptoJS = require("crypto-js");
const fetch = (...args) =>
  import("node-fetch").then(({ default: fetch }) => fetch(...args));

const applicationKey = "abc";
const hmacKey = "xyz";

const json = {
  configuration: {
    text: {
      guides: { enable: true },
      smartGuide: true,
      smartGuideFadeOut: { enable: false, duration: 10000 },
      mimeTypes: ["text/plain", "application/vnd.myscript.jiix"],
      margin: { top: 20, left: 10, right: 10 },
    },
    lang: "en_US",
  },
  xDPI: 96,
  yDPI: 96,
  contentType: "Text",
  theme:
    "ink {\ncolor: #000000;\n-myscript-pen-width: 1;\n-myscript-pen-fill-style: none;\n-myscript-pen-fill-color: #FFFFFF00;\n}\n.math {\nfont-family: STIXGeneral;\n}\n.math-solved {\nfont-family: STIXGeneral;\ncolor: #A8A8A8FF;\n}\n.text {\nfont-family: Open Sans;\nfont-size: 10;\n}\n",
  strokeGroups: [
    {
      penStyle: null,
      strokes: [
        {
          x: [
            151, 151, 151, 151, 150, 150, 150, 149, 149, 148, 147, 147, 146,
            146,
          ],
          y: [26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65],
          t: [
            1654677909781, 1654677909818, 1654677909835, 1654677909848,
            1654677909859, 1654677909868, 1654677909876, 1654677909885,
            1654677909894, 1654677909903, 1654677909913, 1654677909922,
            1654677909932, 1654677909943,
          ],
        },
        {
          x: [166, 166, 166, 165, 165, 165, 164, 164, 163, 163, 162, 161],
          y: [30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63],
          t: [
            1654677910212, 1654677910236, 1654677910249, 1654677910258,
            1654677910266, 1654677910275, 1654677910283, 1654677910292,
            1654677910301, 1654677910309, 1654677910320, 1654677910335,
          ],
        },
        {
          x: [144, 147, 150, 153, 156, 159, 162, 165],
          y: [45, 45, 45, 45, 45, 45, 46, 46],
          t: [
            1654677910586, 1654677910636, 1654677910648, 1654677910655,
            1654677910665, 1654677910673, 1654677910682, 1654677910694,
          ],
        },
        {
          x: [
            169, 172, 175, 178, 181, 184, 184, 182, 179, 176, 173, 170, 167,
            165, 164, 164, 166, 169, 172, 175, 178, 181, 184, 187, 190, 193,
            196, 199, 202, 205, 208, 210, 212, 213, 213, 213, 213, 213, 211,
            208, 205, 202, 199, 196, 193, 190, 188, 186, 184, 183, 182, 182,
            182, 183, 186, 189, 192, 195, 198, 201, 204, 207, 210, 213, 216,
            219, 222, 225, 228, 231, 233, 236, 238, 239, 240, 240, 241, 241,
            240, 237, 234, 231, 228, 225, 222, 219, 216, 213, 211, 210, 209,
            209, 209, 209, 209, 211, 212, 215, 217, 220, 223, 226, 229, 232,
            233,
          ],
          y: [
            46, 47, 47, 47, 47, 47, 44, 41, 40, 39, 39, 39, 39, 42, 45, 48, 51,
            53, 55, 56, 57, 57, 58, 58, 58, 58, 57, 56, 56, 54, 52, 49, 46, 43,
            40, 37, 34, 31, 28, 26, 25, 24, 24, 24, 25, 27, 30, 33, 36, 39, 42,
            45, 48, 51, 54, 56, 58, 58, 59, 59, 59, 59, 59, 59, 59, 58, 58, 56,
            55, 53, 50, 48, 45, 42, 39, 36, 33, 30, 27, 25, 23, 22, 22, 22, 22,
            23, 25, 28, 31, 34, 37, 40, 43, 46, 49, 52, 55, 57, 60, 62, 64, 65,
            65, 65, 62,
          ],
          t: [
            1654677910923, 1654677910940, 1654677910956, 1654677910970,
            1654677910984, 1654677911009, 1654677911034, 1654677911058,
            1654677911070, 1654677911082, 1654677911098, 1654677911110,
            1654677911126, 1654677911145, 1654677911167, 1654677911189,
            1654677911206, 1654677911221, 1654677911236, 1654677911247,
            1654677911258, 1654677911268, 1654677911273, 1654677911278,
            1654677911284, 1654677911290, 1654677911298, 1654677911303,
            1654677911309, 1654677911316, 1654677911325, 1654677911332,
            1654677911341, 1654677911348, 1654677911355, 1654677911361,
            1654677911368, 1654677911377, 1654677911388, 1654677911401,
            1654677911414, 1654677911426, 1654677911437, 1654677911448,
            1654677911458, 1654677911472, 1654677911483, 1654677911494,
            1654677911506, 1654677911516, 1654677911526, 1654677911535,
            1654677911544, 1654677911552, 1654677911564, 1654677911573,
            1654677911582, 1654677911589, 1654677911597, 1654677911603,
            1654677911609, 1654677911615, 1654677911620, 1654677911626,
            1654677911632, 1654677911637, 1654677911643, 1654677911650,
            1654677911656, 1654677911664, 1654677911671, 1654677911678,
            1654677911685, 1654677911691, 1654677911697, 1654677911704,
            1654677911714, 1654677911723, 1654677911735, 1654677911747,
            1654677911756, 1654677911766, 1654677911775, 1654677911783,
            1654677911792, 1654677911803, 1654677911815, 1654677911824,
            1654677911835, 1654677911844, 1654677911852, 1654677911859,
            1654677911866, 1654677911873, 1654677911881, 1654677911889,
            1654677911898, 1654677911906, 1654677911914, 1654677911925,
            1654677911935, 1654677911945, 1654677911957, 1654677911989,
            1654677912015,
          ],
        },
        {
          x: [
            244, 241, 238, 237, 236, 235, 235, 235, 238, 241, 244, 247, 250,
            251, 252, 252, 252, 249, 246, 243, 240, 237, 234,
          ],
          y: [
            44, 44, 46, 49, 52, 55, 58, 61, 63, 63, 64, 63, 60, 57, 54, 51, 48,
            45, 44, 43, 43, 43, 44,
          ],
          t: [
            1654677912196, 1654677912213, 1654677912231, 1654677912241,
            1654677912254, 1654677912266, 1654677912278, 1654677912292,
            1654677912311, 1654677912328, 1654677912341, 1654677912353,
            1654677912370, 1654677912381, 1654677912390, 1654677912401,
            1654677912415, 1654677912430, 1654677912441, 1654677912449,
            1654677912457, 1654677912470, 1654677912498,
          ],
        },
      ],
    },
  ],
  height: 0,
  width: 0,
  conversionState: "DIGITAL_EDIT",
};

const body = JSON.stringify(json).replace(/\n/g, "\\n");

const hmac = new CryptoJS.HmacSHA512(body, applicationKey + hmacKey).toString(
  CryptoJS.Hex
);

async function start() {
  const response = await fetch(
    "https://cloud.myscript.com/api/v4.0/iink/batch",
    {
      headers: {
        accept: "application/json,application/vnd.myscript.jiix", // text/plain for just text
        applicationkey: applicationKey,
        "content-type": "application/json",
        hmac: hmac,
      },
      body: body,
      method: "POST",
    }
  );

  const res = await response.json(); // response.text() in case of text/plain

  console.log(`res => ${JSON.stringify(res)}`);
}

start();

 Thanks Olivier! 

Dear Brhempen,


thank you for the update, I am glad you could get it working.


You explanation will be of help for other developers.


Best regards,


Olivier