获取小程序二维码返回乱码
发布于 5 年前 作者 yonghuang 18005 次浏览 来自 问答

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

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

10 回复

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

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

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
        }
    }

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

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

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

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

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

有用request库吗?

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写请求好一些

回到顶部