Issues using the Batch Export REST API directly without iinkJS
B
Brhempen
started a topic
over 2 years ago
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:
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
B
Brhempen
said
over 2 years ago
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 requestsusing 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:
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:
-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
B
Brhempen
said
over 2 years ago
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.
Reponse still is data => {"timestamp":1654702964552,"status":405,"error":"Method Not Allowed","path":"/api/v4.0/iink/batch"}
Any more suggestions?
O
Olivier @MyScript
said
over 2 years ago
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
B
Brhempen
said
over 2 years ago
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 requestsusing 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:
Brhempen
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:
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?
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:
Thanks Olivier!
- Oldest First
- Popular
- Newest First
Sorted by PopularOlivier @MyScript
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
Brhempen
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:
Reponse still is data => {"timestamp":1654702964552,"status":405,"error":"Method Not Allowed","path":"/api/v4.0/iink/batch"}
Any more suggestions?
Olivier @MyScript
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
Brhempen
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:
Thanks Olivier!
Olivier @MyScript
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