收藏
回答

获取小程序二维码返回乱码

使用官方文档上的方法获取小程序的二维码,返回的是一堆乱码,是因为请求的问题还是官方本身就是如此返回?如果是请求问题,请问该如何修改,如果官方就如此返回,请问该如何处理?

以下是请求和返回的部分截图,跪求官方人员给予解答。



回答关注问题邀请回答
收藏

33 个回答

  • Fe(OH)3
    Fe(OH)3
    2018-01-22

    @.  @8楼 你是怎么处理的呢?使用的是PHP还是node?

    2018-01-22
    有用
    回复
  • R1chardYang
    R1chardYang
    2018-01-22

    @赵子龙 解决了...接口B的page参数只能填已发布的小程序页面

    2018-01-22
    有用
    回复
  • R1chardYang
    R1chardYang
    2018-01-22

    @赵子龙 好吧,我发现了新的问题,返回的是 {"errcode":41030,"errmsg":"invalid page hint: [hyVHjA02194524]"}

    2018-01-22
    有用
    回复
  • Tany
    Tany
    2018-01-22

    public static void post(String urlStr, String filePath){

                

           // 发送请求参数

           JSONObject paramJson = new JSONObject();

           paramJson.put("scene", scene);

           paramJson.put("page", page);

           paramJson.put("width", 430);

           paramJson.put("auto_color", true);


            try {
                URL url = new URL(urlStr);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");// 提交模式
                // conn.setConnectTimeout(10000);//连接超时 单位毫秒
                // conn.setReadTimeout(2000);//读取超时 单位毫秒
                // 发送POST请求必须设置如下两行
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
                printWriter.write(paramJson.toString());
                // flush输出流的缓冲
                printWriter.flush();
                // 开始获取数据
                BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
                File file = new File(filePath);
                if (!file.exists()) {
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    file.createNewFile();
                }
                OutputStream os = new FileOutputStream(file);
                int len;
                byte[] arr = new byte[1024];
                while ((len = bis.read(arr)) != -1) {
                    os.write(arr, 0, len);
                    os.flush();
                }
                os.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        }


    2018-01-22
    有用
    回复
  • 赵青山
    赵青山
    2018-01-22

    打不开的问题多了去了,我之前是用这段代码获取二维码的,没有问题

    2018-01-22
    有用
    回复
  • R1chardYang
    R1chardYang
    2018-01-22

    @赵子龙 没有,用的你上面提供的代码

    2018-01-22
    有用
    回复
  • 赵青山
    赵青山
    2018-01-22

    有用request库吗?

    2018-01-22
    有用
    回复
  • R1chardYang
    R1chardYang
    2018-01-22

    @赵子龙 保存为图片之后打不开是什么问题,写入的数据是buffer

    2018-01-22
    有用
    回复
  • Tany
    Tany
    2018-01-19

    返回的是图片文件, 我一开始也弄错了,

    2018-01-19
    有用
    回复
  • 赵青山
    赵青山
    2018-01-19
    const https = require('https');
    const fs = require('fs');

    //封装请求
    function toRequest(options, postData, callback) {
    var clientrequest = https.request(options, callback || postData);
    clientrequest.on('error', (err) => {
    console.log('请求token出错', err);
    })
    if (typeof postData !== 'function') {
    clientrequest.write(postData);
    }
    clientrequest.end();
    }


    //获取二维码
    function getErWeiMa(token) {
    var postData = JSON.stringify({
    scene: 'hahaha',
    page: 'pages/index/index',
    })
    var options2 = {
    hostname: 'api.weixin.qq.com',
    path: `/wxa/getwxacodeunlimit?access_token=${token}`,
    method: 'POST',
    headers: {
    'Content-Length': postData.length//不写length就报错
           }
    }
    toRequest(options2, postData, function (res) {
    var ws = fs.createWriteStream('erweima.png');
    res.on('data', (chunk) => {
    console.log('正在写入')
    ws.write(chunk);
    })
    res.on('end', (res) => {
    ws.end();
    console.log('写入结束')
    })
    })
    }


    var appid = 'xxxxxxxxxxxxxxxxx';//写入自己的appid
    var secret = 'xxxxxxxxxxxxxxxxx'//写入自己的密钥
    var options1 = {
    hostname: 'api.weixin.qq.com',
    path: `/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`
    }
    //请求获取token
    toRequest(options1, function (res) {
    var data = ""
       res.on('data', (chunk) => {
    data += chunk;
    })
    res.on('end', (res) => {
    var token = JSON.parse(data).access_token
           console.log('获取到token', token);
    getErWeiMa(token);
    })
    })


    //原始node写法,感觉写的不好,不推荐,仅供参考,感觉用request写请求好一些


    2018-01-19
    有用
    回复

正在加载...

登录 后发表内容