某些微信账号upload_media不能成功
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.3k
- Forks
- 1.6k
- PR merge metrics
- No merged PRs in 30d
Description
有些微信账号发送图片的时候无法返回正确的media_id,发送给两个图片服务器都无法获得media_id。
我在用java模拟这一过程的时候也出现过某些账号无法获得media_id的情况,但这些账号在python的wxBot中又是可以获得media_id的,而在wxBot中无法获得media_id的账号,在java程序中又可以获得media_id。
java代码如下:
private String uploadImageToUrl(String filepath,String toUser,String uri) {
String result = null;
try {
File file = new File(filepath);
String md5 = getMd5ByFile(file);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
```
String localId = getMsgId();
URL url = new URL(uri + "?f=json");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setConnectTimeout(10000);
con.setReadTimeout(60000);
con.setRequestProperty("Accept", "*/*");
con.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
con.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
con.setRequestProperty("DNT", "1");
con.setRequestProperty("User-Agent",
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36");
String boundary = "----WebKitFormBoundary" + getRandomString(16);
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
String requestString = "{\"UploadType\":2,\"BaseRequest\":{\"Uin\":" + uin.intValue() + ",\"Sid\":\"" + sid
+ "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"" + deviceId + "\"},\"ClientMediaId\":" + localId
+ ",\"TotalLen\":" + file.length() + ",\"StartPos\":0,\"DataLen\":" + file.length()
+ ",\"MediaType\":4,\"FromUserName\":\"" + fromUser + "\",\"ToUserName\":\"" + toUser
+ "\",\"FileMd5\":\"" + md5 + "\"}";
StringBuilder sb = new StringBuilder();
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"id\"\r\n\r\n");
sb.append("WU_FILE_0\r\n");
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"name\"\r\n\r\n");
sb.append(file.getName() + "\r\n");// debug
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"type\"\r\n\r\n");
sb.append("image/jpeg\r\n");
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"lastModifiedDate\"\r\n\r\n");
sb.append(getGMT(new Date(file.lastModified())) + "\r\n");// debug
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"size\"\r\n\r\n");
sb.append(file.length() + "\r\n");
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"mediatype\"\r\n\r\n");
sb.append("pic\r\n");
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"uploadmediarequest\"\r\n\r\n");
sb.append(requestString + "\r\n");// debug
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"webwx_data_ticket\"\r\n\r\n");
sb.append(webticket + "\r\n");
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"pass_ticket\"\r\n\r\n");
sb.append(passTicket + "\r\n");
sb.append("--").append(boundary).append("\r\n");
sb.append("Content-Disposition: form-data; name=\"filename\"; filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Type: image/jpeg\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
OutputStream out = new DataOutputStream(con.getOutputStream());
out.write(head);
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
byte[] foot = ("\r\n--" + boundary + "--\r\n").getBytes("utf-8");
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
// 定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
System.out.println("发送POST请求出现异常!" + e);
e.printStackTrace();
throw new IOException("数据读取异常");
} finally {
if (reader != null) {
reader.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(result);
JSONObject obj = new JSONObject(result);
return obj.getString("MediaId");
}
```
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start from the wxBot upload_media entry point and compare its media upload request and response handling with the Java example in the issue. Reproduce the failure for an affected account, inspect the returned response for media_id, and determine a consistent upload result across accounts; the issue is resolved when media_id is returned correctly or the account-specific limitation is documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100