chaosss 2022-11-04 13:36:30

Bmob加密请求

function onRequest(request, response, modules) {
  var host = "https://api.example.com";
  var url = host + request.query.path;
  var timeStamp = Date.now();
  var noncestr = "";
  var secretKey = "应用密钥";
  var safeToken = "API安全码";
  var safeSign = "";

  /*生成N位随机流水号*/
  /*默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1*/
  var n = 16;
  var $chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
  var maxPos = $chars.length;
  for (i = 0; i < n; i++) {
    noncestr += $chars.charAt(Math.floor(Math.random() * maxPos));
  }

  //获取crypto模块
  var crypto = modules.oCrypto;
  var md5 = crypto.createHash("md5");

  safeSign = md5
    .update(String(request.query.path + timeStamp + safeToken + noncestr))
    .digest("hex");


  //获取Http模块
  var http = modules.oHttp;

  var options = {
    url: url,
    headers: {
      "X-Bmob-SDK-Type": "API",
      "X-Bmob-Safe-Sign": safeSign,
      "X-Bmob-Safe-Timestamp": timeStamp,
      "X-Bmob-Noncestr-Key": noncestr,
      "X-Bmob-Secret-Key": secretKey,
      "Content-Type": "application/json"
    }
  };

  //response.send(options);

  //发起Get请求
  http(options, function (error, res, body) {
    response.send(body);
  });
}

评论

ホーム - Wiki
Copyright © 2011-2024 iteam. Current version is 2.137.1. UTC+08:00, 2024-11-15 00:28
浙ICP备14020137号-1 $お客様$