【快应用】搭建数据交换的桥梁--不同快应用的数据共享案例

发布时间 2023-03-22 21:13:18作者: 华为开发者论坛

【关键词】

数据交换,签名指纹,快应用

 

【问题描述】

不同快应用之间的数据交换,调用exchange. get获取数据时出现报错提示,sign参数不填提示202参数错误,sign参数填入发布方快应用的签名证书后,仍是提示200错误,该如何处理?

接口调用:

  exchange.get({
        package: 'com.huawei.1104',
        sign: "538dc4a54b0ed1def37fd0bdfe064178a7e532d832e74dd7013485841acf5d81",
        key: 'test2',
        scope: "application",
        success: function (ret) {
          console.log(`handling success, value = ${ret}`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })

证书:

cke_812.png

报错截图:

cke_1815.png

 

【问题分析】

此问题的原因时sign参数填写不对导致的,exchange接口中的sign参数,描述是使用 SHA-256 加密的数据发布方签名,很多时候会理解成华为ide生成的指纹证书,其实是不对的,这里的sign参数是应填写pkg.getSignatureDigests接口返回的signatureDigests(使用SHA-256算法处理后的签名信息列表)。这样才能成功调用exchange.get接口获取到共享数据。

 

【解决方案】

在调用get接口时,需要先拿到数据发布方的签名信息,同时还需要确保数据发布方有授权对应应用去获取否则也会调用失败的。

数据发布方接口调用流程:

Step1、exchange.set接口设置数据。

    set() {
      exchange.set({
        package: 'com.huawei.1104',
        sign: 'ddba5db57dbf3357277b05fa7781c197425c957c3d71fb3b0c99350d188bca9a',
        key: 'test3',
        scope: "application",
        value: 'testmessage',
        success: function () {
          console.log(`handling success`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })
    },

Step2、pkg.getSignatureDigests接口拿到被授权应用的签名信息。

    getSign() {
      var that = this
      app.getPackageInfo({
        packageName: 'com.huawei.exchange',
        success: function (data) {
          console.log("APP getPackageInfo is : " + data.signatures);
          that.info = data.signatures;
        },
      })
    }

Step 3、exchange.grantPermission授权对应应用获取。

    grantPermission() {
      var that = this
      exchange.grantPermission({
        package: 'com.huawei.exchange',
        sign: that.info,
        writable: true,
        success: function () {
          console.log(`handling success`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })
    },

被授权应用获取流程:

Step1、 pkg.getSignatureDigests接口拿到数据发布方应用的签名信息。

    bGetSign() {
      var that = this
      console.log(`handling fail, `)
      app.getPackageInfo({
        packageName: 'com.huawei.1104',
        success: function (data) {
          console.log("APP getPackageInfo is : " + data.signatures);
          that.info = data.signatures;
          that.get_success = data;
        },
      })
    },

Step2、exchange.get接口获取数据发布方的共享数据。

    bGet() {
      var that = this
      exchange.get({
        package: 'com.huawei.1104',
        sign: that.info,
        key: 'test3',
        scope: "application",
        success: function (ret) {
          console.log(`handling success, value = ${ret}`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })

成功拿到发布方共享数据

cke_11867.png

 

【Demo】

发布方快应用代码:

<template>
  <div style="flex-direction: column">
    <input type="button" class="btn" value="set" onclick="set" />
    <input type="button" class="btn" value="signatures" onclick="getSign" />
    <input type="button" class="btn" value="grantPermission" onclick="grantPermission" />
  </div>
</template>
<script>
  import exchange from '@service.exchange';
  import app from '@system.app';
  export default {
    set() {
      exchange.set({
        package: 'com.huawei.1104',
        sign: 'ddba5db57dbf3357277b05fa7781c197425c957c3d71fb3b0c99350d188bca9a',
        key: 'test3',
        scope: "application",
        value: 'testmessage',
        success: function () {
          console.log(`handling success`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })
    },
    grantPermission() {
      var that = this
      exchange.grantPermission({
        package: 'com.huawei.exchange',
        sign: that.info,
        writable: true,
        success: function () {
          console.log(`handling success`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })
    },
    getSign() {
      var that = this
      app.getPackageInfo({
        packageName: 'com.huawei.exchange',
        success: function (data) {
          console.log("APP getPackageInfo is : " + data.signatures);
          that.info = data.signatures;
        },
      })
    }
  }

</script> 
<style >
  .btn {
    width: 80%;
    margin-top: 10px;
    background-color: #00bfff;
  }
</style>

获取方快应用代码:

<template>
  <!-- Only one root node is allowed in template. -->
  <div class="container">
    <input type="button" class="btn" value="signatures" onclick="bGetSign" />
    <input type="button" class="btn" value="getData" onclick="bGet" />
  </div>
</template>
<style>
  .container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
</style>
<script>
  import exchange from '@service.exchange';
  import app from '@system.app';
  module.exports = {
    data: {
      componentData: {},
    },
    bGetSign() {
      var that = this
      app.getPackageInfo({
        packageName: 'com.huawei.1104',
        success: function (data) {
          console.log("APP getPackageInfo is : " + data.signatures);
          that.info = data.signatures;
          that.get_success = data;
        },
      })
    },
    bGet() {
      var that = this
      exchange.get({
        package: 'com.huawei.1104',
        sign: that.info,
        key: 'test3',
        scope: "application",
        success: function (ret) {
          console.log(`handling success, value = ${ret}`)
        },
        fail: function (data, code) {
          console.log(`handling fail, code = ${code}`)
        }
      })
    },
  }
</script>

 欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh