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

小程序 文章 2021-02-09 14:21 1180 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


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
IOS端 当Promise与异步函数一起使用时 特定情况下永远不会settled
我真的是服了,我明明按照小程序注册流程走的,注册完后,创建项目却发现小程序id变成了小游戏id?
小游戏经常报错,但是错误信息是空,是怎么回事?
微信小程序调用接口无法获取数据?
列表渲染,怎么分类?
随便看看
企业微信通讯录账号被管理员误/恶意删除,怎么办? 8517
问题? 8138
如何快速搭建抽奖助手小程序(无需代码知识) 5295
许涛 大哥在吗, 要解冻小程序的时候提示信息主体不一致, 能帮忙看下吗? 6566
小程序搜一搜全称搜索不显示 麻烦解决一下!谢谢 5855
我的小程序广告收款主体是个体工商户,每个月邮寄两次发票很是麻烦,我年底的时候打包邮寄一次可以么? 5517
如何解决渲染层网络错误Failed to load media? 16763
小程序找号找回 释放昵称? 2778
我的小程序广告主收入5301,实际进账4406,是怎么回事呀? 2649
删除部分微信用户个人数据的通知,怎么删除? 5823