小程序
小游戏
企业微信
微信支付
扫描小程序码分享
使用官方文档上的方法获取小程序的二维码,返回的是一堆乱码,是因为请求的问题还是官方本身就是如此返回?如果是请求问题,请问该如何修改,如果官方就如此返回,请问该如何处理?
以下是请求和返回的部分截图,跪求官方人员给予解答。
33 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
@. @8楼 你是怎么处理的呢?使用的是PHP还是node?
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
@赵子龙 解决了...接口B的page参数只能填已发布的小程序页面
@赵子龙 好吧,我发现了新的问题,返回的是 {"errcode":41030,"errmsg":"invalid page hint: [hyVHjA02194524]"}
public static void post(String urlStr, String filePath){
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(
// 获取URLConnection对象对应的输出流
PrintWriter printWriter =
PrintWriter(httpURLConnection.getOutputStream());
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
// 开始获取数据
BufferedInputStream bis =
BufferedInputStream(httpURLConnection.getInputStream());
File file =
File(filePath);
if
(!file.exists()) {
(!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
file.createNewFile();
OutputStream os =
FileOutputStream(file);
int
len;
byte
[] arr =
[
1024
];
while
((len = bis.read(arr)) != -
1
) {
os.write(arr,
0
, len);
os.flush();
os.close();
catch
(Exception e) {
// TODO: handle exception
打不开的问题多了去了,我之前是用这段代码获取二维码的,没有问题
@赵子龙 没有,用的你上面提供的代码
有用request库吗?
@赵子龙 保存为图片之后打不开是什么问题,写入的数据是buffer
返回的是图片文件, 我一开始也弄错了,
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';//写入自己的appidvar secret = 'xxxxxxxxxxxxxxxxx'//写入自己的密钥var options1 = { hostname: 'api.weixin.qq.com', path: `/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`}//请求获取tokentoRequest(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写请求好一些
正在加载...
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
@. @8楼 你是怎么处理的呢?使用的是PHP还是node?
@赵子龙 解决了...接口B的page参数只能填已发布的小程序页面
@赵子龙 好吧,我发现了新的问题,返回的是 {"errcode":41030,"errmsg":"invalid page hint: [hyVHjA02194524]"}
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
}
}
打不开的问题多了去了,我之前是用这段代码获取二维码的,没有问题
@赵子龙 没有,用的你上面提供的代码
有用request库吗?
@赵子龙 保存为图片之后打不开是什么问题,写入的数据是buffer
返回的是图片文件, 我一开始也弄错了,
//原始node写法,感觉写的不好,不推荐,仅供参考,感觉用request写请求好一些