Advertisement
Guest User

Vuforia AddTarget

a guest
May 16th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by mbellocchi on 16/05/2017.
  3.  */
  4.  
  5. var request = require('request');
  6. var fs = require('fs');
  7. var path = require('path');
  8. var crypto = require('crypto');
  9.  
  10. var collectionPath = "../public/collections/56781ea712f7a8e414626754/";
  11. var folderMetadata = "3D/representativeMetadata/";
  12. var folderImgs = "3D/00001/";
  13. var img = "22264330492_8396e58b54_720_26734084@N04";
  14.  
  15. // Set the headers
  16.  
  17. var server_secret_key = "my_server_secret_key"
  18.  
  19.  
  20. const HTTPVerb = "POST";
  21. const ContentType = "multipart/form-data";
  22. const date = new Date();
  23. const RequestPath = "https://vws.vuforia.com/targets";
  24.  
  25. var optionsOld = {
  26.     url: RequestPath,
  27.     method: 'POST',
  28.     form: {
  29.         "image": fs.createReadStream(path.join(__dirname, collectionPath + folderImgs + img + ".jpg")).toString('base64'),
  30.         "name": img.split("@")[0],
  31.         "width": 500.0,
  32.         "application_metadata": fs.createReadStream(path.join(__dirname, collectionPath + folderMetadata + img.split("@")[0] + ".txt")).toString('base64')
  33.     }
  34. }
  35.  
  36. const ContentMD5 = crypto.createHash('md5').update(optionsOld.toString()).digest("hex");
  37.  
  38. const StringToSign =
  39.     HTTPVerb + "\n" +
  40.     ContentMD5 + "\n" +
  41.     ContentType + "\n" +
  42.     date + "\n" +
  43.     RequestPath;
  44.  
  45.  
  46. const hmac = crypto.createHmac('sha1', server_secret_key);
  47. hmac.update(StringToSign);
  48.  
  49. //console.log(hmac.digest('hex'));
  50. //console.log(hmac.digest('base64'));
  51.  
  52. var Signature = hmac.digest('base64');
  53.  
  54.  
  55. var AuthorizationValue  = 'VWS '+ server_secret_key + ':' + Signature;
  56.  
  57. var headers = {
  58.     'Content-Type':     'application/json',
  59.     'Authorization':    AuthorizationValue
  60. }
  61.  
  62. var options = {
  63.     url: RequestPath,
  64.     method: 'POST',
  65.     headers: headers,
  66.     form: {
  67.         "image": fs.createReadStream(path.join(__dirname, collectionPath + folderImgs + img + ".jpg")).toString('base64'),
  68.         "name": img.split("@")[0],
  69.         "width": 500.0,
  70.         "application_metadata": fs.createReadStream(path.join(__dirname, collectionPath + folderMetadata + img.split("@")[0] + ".txt")).toString('base64')
  71.     }
  72. }
  73.  
  74. console.log(AuthorizationValue);
  75.  
  76. // Configure the request
  77.  
  78. // Start the request
  79. request(options, function (error, response, body) {
  80.     console.log(response.statusCode);
  81.     if (!error && response.statusCode == 200) {
  82.         // Print out the response body
  83.         console.log(body);
  84.     } else {
  85.         console.log("Error", error);
  86.     }
  87. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement