如何通过云函数访问移动OneNet?

小程序 文章 2021-02-09 14:21 1111 0 全屏看文

AI助手支持GPT4.0

如何通过云函数访问移动OneNet?How to access mobile OneNet through cloud functions?

如何通过云函数访问移动OneNet?

大家好!我在通过小程序访问外网数据时,遇到了问题,访问的网址是移动OneNet平台,我已经设置了request域名为 https://api.heclouds.com,情况如下:

1、在小程序端,直接使用wx.request访问,没有问题,代码如下:

wx.request({

            url: "https://api.heclouds.com/devices/"+ID+"/datapoints",

            header: {

             "api-key": "*********="

             },

             success (res) {

               console.log("Net:",res.data);

             }

})

2、当我将代码改为通过云函数访问时,采用的是request-promise模块,始终报错:

{"errno":6,"error":"invalid parameter: auth by api-key or authorization"}

代码如下:

var rp=require("request-promise");

        var option={

          url: "https://api.heclouds.com/devices/12345678/datapoints",

         method: "GET",

         json: true,

          qs: {

            "api-key": "******="

          },

          header: {

           "content-type": "application/json",

            "User-Agent": "Request-Promise"            

          }

       }; 

       await rp(option)

        .then(function (repos) {

          console.log('User has repos', repos.length);

        })

3、我测试了request和requset-promise两种访问方式,都一样不行。

4、我测试了安装wx-server-sdk,再安装request和request-promise,不行。

5、我测试了只安装wx-server-sdk,并使用它自带的request版本,也不行。

6、移动的api-key 字符串里面有一个等号字符,传输时变成%3D,是不是这个字符的问题?但在小程序端又是可以的。

还望大家不吝赐教,谢谢!

hello everyone! I encountered a problem when accessing the Internet data through the small program. The website I visited is the mobile onenet platform. I have set the domain name of request as https://api.heclouds.com The situation is as follows: 1 wx.request The code is as follows: wx.request ({ url: " https://api.heclouds.com/devices/ "+ID+"/datapoints", header: { "api-key": "*********=" }, success (res) { console.log ("Net:", res.data );}}) 2. When I change the code to access through the cloud function, I use the request promise module and always report an error: {"errno": 6, "error": "invalid parameter: auth by API key or authorization"} the code is as follows: VAR RP = require ("request promise"); var option={ url: " https://api.heclouds.com/devices/12345678/datapoints ", method: "GET", json: true, qs: { "api-key": "******=" }, header: { "content-type": "application/json", "User-Agent": "Request-Promise" } }; await rp(option) .then(function (repos) { console.log ('User has repos', repos.length ); }(3) I have tested the two access methods of request and requse promise, but they are all the same. 4. I tested the installation of Wx server SDK, and then the installation of request and request promise, No. 5. I tested only installing Wx server SDK and using its own request version, but it didn't work. 6. There is an equal sign character in the mobile API key string, which becomes% 3D during transmission. Is this a problem? But in the small program side is OK. I hope you will give me your advice. Thank you!

回答:

逍遥:

一开始写在header里的,不行,才换到qs里的。

跨商通:

你在wx.request里写在header里,为什么到了request-promise里就换成qs里呢?

Mr.Zhao:

小程序写到header里面,用request-promise 你就写到qs querystring里面?

-EOF-

AI助手支持GPT4.0