leancloud 的云引擎如何访问微信的服务器?

我需要访问微信服务器获取token来生成二维码。小程序不允许把secret保存在前端,需要一个后台访问微信服务器。我基本没有后台知识。请问云函数怎么写,才能访问微信服务器呢?请用js的方法。谢谢。

微信的地址是这样的。
https请求方式: GET
https://api.weixin.qq.com/cgi...


var request1 = require('request');

request1({
  method: 'GET',
  url: 'https://www.baidu.com',
  json: {
    title: 'Vote for Pedro',
    body: 'If you vote for Pedro, your wildest dreams will come true'
  }
}, function(err, res, body) {
   
  if (err) {
    console.error('Request failed with response code ' + res.statusCode);
    return"haha error";
  } else {
     
    console.log(body);
     return "success";
  }
});`


我吧上面的代码粘到云函数里面,结果是{}空的。好像是不能识别request

阅读 1.3k
1 个回答

云函数的request和你这个request是不同的。云函数的request指的内部封装好的对象。

你这里的request指的是nodejs里的request模块。

习惯用axios,这里用axios大概写个例子吧

npm install axios --save
AV.Cloud.define('getData', function(request) {
 axios.get('https://api.weixin.qq.com/cgi-bin/token', {
  params: { 
     'grant_type': 'client_credential',
     'appid': 'xxxxxxxxxxxxxxxxxxx',
     'secret': 'xxxxxxxxxxxxxxxxxx',    
   }
}).then(function (response) {
    // 成功
}).catch(function (error) {
  // 失败
    alert(error);
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题