普通代云开发,云函数上传已经按照文档进行签名,提示签名不合法?
发布于 5 年前 作者 pjin 8591 次浏览 来自 官方Issues

我按照以下的流程使用微信普通代云开发

1. 开通了云开发。小程序appid:wxb2d34f69d701e47e

2. 创建了云环境。环境id:xmd-cloud-dev1447e,环境别名xmd-cloud1447e

3. 可以通过接口查询到已经创建了云环境。https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/cloudbase/common/getEnvInfo.html

4. 创建了云函数。环境id:xmd-cloud-dev1447e,function_name:openMini2

5. 可以通过接口查询到云函数已经创建成功。https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/Mini_Programs/cloudbase/common/listFunctions.html

6. 获取到代码保护的秘钥。nGmQniR0v+e7wizy9alZqD40Bpc3ybfA8lP2IR2MJPR5BH5mLy5lxGd0gh2K7i3b

7. 根据要求的签名算法生成了秘钥hashed_payload 。

要签名的明文是{"CodeSecret":"nGmQniR0v+e7wizy9alZqD40Bpc3ybfA8lP2IR2MJPR5BH5mLy5lxGd0gh2K7i3b","EnvId":"xmd-cloud-dev1447e","FunctionName":"openMini2","Handler":"index.main","ZipFile":"UEsDBBQAAAAIAEBaYlJLkjQyAwIAADgDAAAIAAAAaW5kZXguanNlUsFu00AQvfsrRr3YFomTCqiqSOUCHDgXVK4re5IsxGt3d+040EgIBEklqgpQEUgRPaByo1RCqkJKxc/YBk78Amuvk7RgaeXdmTfvzezbRgPS2atsdJ4fnGTPj7L9j/nbUXp2argBExLcXhB5sAEctyPK0TL7SV0gj5HXhffQtA2jRDiUUWk9NgCQxS0wE9+rl4m6h3F9vRN7g9X4Ea5dvb7utq+ZNWOoKv+X1nsDkzDgUjg+oUxpEzFgLlgYI5M1UH1JTKQNGzegENR99pObOq7wuqMOyq37VdBSagCiT6XbrYgc4koaMLvkUCxEIJiq5h7vbbpd9NFsVSlQw8uIM7iY1SR2CRgaxTIWOFNTAwsktIOIeaah0nqKdsR08hJZEBYxoZuRfFD+56NxFFGvmIv0Ca0ccY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AA=="}

生成签名的代码是

其中str为JSON.stringify()后得到的字符串

private String getEncryptCode() throws Exception {
   String str = "{\"CodeSecret\":\"nGmQniR0v+e7wizy9alZqD40Bpc3ybfA8lP2IR2MJPR5BH5mLy5lxGd0gh2K7i3b\",\"EnvId\":\"xmd-cloud-dev1447e\",\"FunctionName\":\"openMini2\",\"Handler\":\"index.main\",\"ZipFile\":\"UEsDBBQAAAAIAEBaYlJLkjQyAwIAADgDAAAIAAAAaW5kZXguanNlUsFu00AQvfsrRr3YFomTCqiqSOUCHDgXVK4re5IsxGt3d+040EgIBEklqgpQEUgRPaByo1RCqkJKxc/YBk78Amuvk7RgaeXdmTfvzezbRgPS2atsdJ4fnGTPj7L9j/nbUXp2argBExLcXhB5sAEctyPK0TL7SV0gj5HXhffQtA2jRDiUUWk9NgCQxS0wE9+rl4m6h3F9vRN7g9X4Ea5dvb7utq+ZNWOoKv+X1nsDkzDgUjg+oUxpEzFgLlgYI5M1UH1JTKQNGzegENR99pObOq7wuqMOyq37VdBSagCiT6XbrYgc4koaMLvkUCxEIJiq5h7vbbpd9NFsVSlQw8uIM7iY1SR2CRgaxTIWOFNTAwsktIOIeaah0nqKdsR08hJZEBYxoZuRfFD+56NxFFGvmIv0Ca0ccYIQGQmpE/GeKDnUvAw5kWjNuwZ4EPnhVkKWgxRfSGS3BZWiU5xgZwdWVmoXMNsR8sESVB43Jaes8y92uNwqO7NPT/MPk/zZ13S69+vHKJ8cZuP3P8+P0+kM2qQn8M/3l9m302z/S3p2lH1+l41PlN/53nE2e7PgoeJ2Eqq31tIllwTS6ZNs/OL368N8spsfjJXAIotl0V3qq8KQcIF3mLRuqQtxWNC3bGjAarPZhCuw1rTnnENtYOWbvujCTPUc9EvhXHuywKhI6fbwL1BLAwQUAAAACAD7dTtScboMeKsAAAADAQAADAAAAHBhY2thZ2UuanNvbk2POw/CMAyE9/4KK0MnGrVl64oYGJhYu1SJUc0jqeIAlVD57eQBgtHfnX3nZwEgzHBF0YGwE5o9GRKrSO/omKyJQiNrWWeqkZWjyX+UDK8DpYmMxlmeONNs5CDElAA8so82VKOFXmyds64DYyEKwBMqOhLqXkBZAs7koRFhc0nXhpsfrftlXkih4dR7d9h8y4UPNBpF+Bf7mCtGF96pWJ+j/9XKtWzT6WIp3lBLAwQUAAAACAB6iGFS2PDAOUEAAABIAAAACwAAAGNvbmZpZy5qc29uq+ZSUFAqSC3KzSwuzszPK1ayUgAJAQXzC1LzEgsygQLRXJycSqVFOcXJGam5qXrpqXmpRYklqUpgdbFAsparFgBQSwECHwAUAAAACABAWmJSS5I0MgMCAAA4AwAACAAkAAAAAAAAACAAAAAAAAAAaW5kZXguanMKACAAAAAAAAEAGACiFSynEg/XAaIVLKcSD9cBhrtTNEsO1wFQSwECHwAUAAAACAD7dTtScboMeKsAAAADAQAADAAkAAAAAAAAACAAAAApAgAAcGFja2FnZS5qc29uCgAgAAAAAAABABgAAAmXVnj01gGMRFo0Sw7XAUJLWDRLDtcBUEsBAh8AFAAAAAgAeohhUtjwwDlBAAAASAAAAAsAJAAAAAAAAAAgAAAA/gIAAGNvbmZpZy5qc29uCgAgAAAAAAABABgAFgzizHkO1wEWDOLMeQ7XATRzUTRLDtcBUEsFBgAAAAADAAMAFQEAAGgDAAAAAA==\"}";
   MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
   messageDigest.update(str.getBytes());
   return byte2Hex(messageDigest.digest());
}

private static String byte2Hex(byte[] bytes) {
   StringBuffer stringBuffer = new StringBuffer();
   String temp = null;
   for (int i = 0; i < bytes.length; i++) {
      temp = Integer.toHexString(bytes[i] & 0xFF);
      if (temp.length() == 1) {
         //1得到一位的进行补0操作
         stringBuffer.append("0");
      }
      stringBuffer.append(temp);
   }
   return stringBuffer.toString();
}

得到的签名是7ba7a00df8250f5c0e30f0f4a39935fbb1ee1f718d0299caabf52f9a1b318364

8. 获得到上传云函数的凭证为

Authorization:TC3-HMAC-SHA256 Credential=AKIDl7K9-ILOfko82nS6nHAZCssuPn_hgYtTyTJXfMzFvGnzBCQx-HzeRh5I2AProWqM/2021-03-08/scf/tc3_request, SignedHeaders=content-type;host, Signature=2180c12b81cb9a32973a57fcdd77411f9ab9ad89bce43f8500635e35399fc16c

Host:scf.tencentcloudapi.com

Content-Type:application/json

X-TC-Action:UpdateFunctionCode

X-TC-Timestamp:1615178085

X-TC-Version:2018-04-16

X-TC-Region:ap-shanghai

9. 用这个得到的heads和postData去上传云函数代码。得到返回结果:

post的代码如下:

    private void postUpload(String accessToken, String headStr) throws Exception {
      String url = "https://scf.tencentcloudapi.com?access_token=" + accessToken;
      String body = "{\"CodeSecret\":\"nGmQniR0v+e7wizy9alZqD40Bpc3ybfA8lP2IR2MJPR5BH5mLy5lxGd0gh2K7i3b\",\"EnvId\":\"xmd-cloud-dev1447e\",\"FunctionName\":\"openMini2\",\"Handler\":\"index.main\",\"ZipFile\":\"UEsDBBQAAAAIAEBaYlJLkjQyAwIAADgDAAAIAAAAaW5kZXguanNlUsFu00AQvfsrRr3YFomTCqiqSOUCHDgXVK4re5IsxGt3d+040EgIBEklqgpQEUgRPaByo1RCqkJKxc/YBk78Amuvk7RgaeXdmTfvzezbRgPS2atsdJ4fnGTPj7L9j/nbUXp2argBExLcXhB5sAEctyPK0TL7SV0gj5HXhffQtA2jRDiUUWk9NgCQxS0wE9+rl4m6h3F9vRN7g9X4Ea5dvb7utq+ZNWOoKv+X1nsDkzDgUjg+oUxpEzFgLlgYI5M1UH1JTKQNGzegENR99pObOq7wuqMOyq37VdBSagCiT6XbrYgc4koaMLvkUCxEIJiq5h7vbbpd9NFsVSlQw8uIM7iY1SR2CRgaxTIWOFNTAwsktIOIeaah0nqKdsR08hJZEBYxoZuRfFD+56NxFFGvmIv0Ca0ccYIQGQmpE/GeKDnUvAw5kWjNuwZ4EPnhVkKWgxRfSGS3BZWiU5xgZwdWVmoXMNsR8sESVB43Jaes8y92uNwqO7NPT/MPk/zZ13S69+vHKJ8cZuP3P8+P0+kM2qQn8M/3l9m302z/S3p2lH1+l41PlN/53nE2e7PgoeJ2Eqq31tIllwTS6ZNs/OL368N8spsfjJXAIotl0V3qq8KQcIF3mLRuqQtxWNC3bGjAarPZhCuw1rTnnENtYOWbvujCTPUc9EvhXHuywKhI6fbwL1BLAwQUAAAACAD7dTtScboMeKsAAAADAQAADAAAAHBhY2thZ2UuanNvbk2POw/CMAyE9/4KK0MnGrVl64oYGJhYu1SJUc0jqeIAlVD57eQBgtHfnX3nZwEgzHBF0YGwE5o9GRKrSO/omKyJQiNrWWeqkZWjyX+UDK8DpYmMxlmeONNs5CDElAA8so82VKOFXmyds64DYyEKwBMqOhLqXkBZAs7koRFhc0nXhpsfrftlXkih4dR7d9h8y4UPNBpF+Bf7mCtGF96pWJ+j/9XKtWzT6WIp3lBLAwQUAAAACAB6iGFS2PDAOUEAAABIAAAACwAAAGNvbmZpZy5qc29uq+ZSUFAqSC3KzSwuzszPK1ayUgAJAQXzC1LzEgsygQLRXJycSqVFOcXJGam5qXrpqXmpRYklqUpgdbFAsparFgBQSwECHwAUAAAACABAWmJSS5I0MgMCAAA4AwAACAAkAAAAAAAAACAAAAAAAAAAaW5kZXguanMKACAAAAAAAAEAGACiFSynEg/XAaIVLKcSD9cBhrtTNEsO1wFQSwECHwAUAAAACAD7dTtScboMeKsAAAADAQAADAAkAAAAAAAAACAAAAApAgAAcGFja2FnZS5qc29uCgAgAAAAAAABABgAAAmXVnj01gGMRFo0Sw7XAUJLWDRLDtcBUEsBAh8AFAAAAAgAeohhUtjwwDlBAAAASAAAAAsAJAAAAAAAAAAgAAAA/gIAAGNvbmZpZy5qc29uCgAgAAAAAAABABgAFgzizHkO1wEWDOLMeQ7XATRzUTRLDtcBUEsFBgAAAAADAAMAFQEAAGgDAAAAAA==\"}";

      Map<String, String> headers = new HashMap<>();

//    String headerStr = "Authorization:TC3-HMAC-SHA256 Credential=AKID3oEXbr4ksVEPsGsBzfa8o7uhGHiplLnds5IVbC5zCnF2HrVUvPKc4zFqvov0zRS4/2021-03-04/scf/tc3_request, SignedHeaders=content-type;host, Signature=cbd97bec7120703c82339ba085e1f243bc3c282ac837253a62e44d82e24709fb\r\nHost:scf.tencentcloudapi.com\r\nContent-Type:application/json\r\nX-TC-Action:UpdateFunctionCode\r\nX-TC-Timestamp:1614828707\r\nX-TC-Version:2018-04-16\r\nX-TC-Region:ap-shanghai\r\nX-TC-Token:4LEQkqsKxIpl4hCd7OXBLlF5W6pwoJXa8c55113e1288d2f58649eeaf471ba715_lAO_txVvaVzMI0bqshjciL17ZcwyLvhnLseIjcy77LgObtzuWYgRnXL772W0qgT4BcytXtWBYA_lF2dXxFhNVVsu9ypttmEcFGRxXWmOky2iydUYBqtpI_iElAk6RYAc62UozHwkJxldxRd9jyHDqFhadvChSUG4YwJ5gNg9Uxar3LZtZLEmxzSxX4tNEtFkhVzSmiVeZqU4-cFBaMe4P8gqLrP9DatFb8_mCsHAVT3BeY3j6U7563ecOoo5Duzu_JUdllGvsnJkbrBh26qgz23PvONohyt7OII92f-j5KPfns-RoG3kVO3HIeCKWe-zSyYdcmqjEgRdAO5P0yzUx7GZlM12E45Cf9UZrLYDCM";
      String[] strs = headStr.split("\r\n");
      for (int i = 0; i <= strs.length - 1; i++) {
         String each = strs[i];
         String[] s = each.split(":");
         String value = s[1];
//       if (s[0].equals("Authorization")) {
//          s[1] = s[1] + "a";
//       }
         headers.put(s[0], value);
      }


{"Response":{"Error":{"Code":"AuthFailure.SignatureFailure","Message":"The provided credentials could not be validated. Please check your signature is correct."},"RequestId":"98da9903-afc3-4877-9245-59bd6a646d60"}}


这里提示的是签名不合法。可是生成签名的代码已经是用的文档提供的签名代码。java和js执行的结果是一致的。已经试了很多次了,希望能得到帮助。

回到顶部