小程序
小游戏
企业微信
微信支付
扫描小程序码分享
https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html
请求地址:POST https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=ACCESS_TOKEN
6 个回答
加粗
标红
插入代码
插入链接
插入图片
上传视频
代码已测,已通。关注WX公众号,回复 手机号 ,直接给java代码,还有更多的技术类干货等你哦~
你好,麻烦通过点击下方“反馈信息”按钮,提供出现问题的。
token写到url后面,code用json
此接口需要付费使用吗?我每次使用需要验证码可是没能获取到验证码,是不是购买短信接口啥的
package servlet; import com.alibaba.fastjson.JSON; import net.sf.json.JSONObject; import org.apache.http.Consts; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.SocketTimeoutException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Set; public class WXPhoneServlet2 extends HttpServlet { String js_code; PrintWriter out; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); js_code = request.getParameter("code"); out = response.getWriter(); String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=yourtooken"; Map<String, String> map = new HashMap<>(1); map.put("code", js_code); String res = WXPhoneServlet2.postJson(url, JSON.toJSONString(map), null); out.write("" + res); } public static String postJson(String url, String json, Map<String, String> headMap) { String returnStr; CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; try { HttpPost post = new HttpPost(url); post.setConfig(RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build()); httpClient = HttpClientBuilder.create().build(); StringEntity s = new StringEntity(json, Consts.UTF_8); s.setContentType("application/json"); if (headMap != null && headMap.size() > 0) { Set<String> keySet = headMap.keySet(); for (String key : keySet) { post.addHeader(key, headMap.get(key)); } } post.setEntity(s); httpResponse = httpClient.execute(post); BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name())); StringBuilder stringBuffer = new StringBuilder(100); String str; while ((str = reader.readLine()) != null) { stringBuffer.append(str); } returnStr = stringBuffer.toString(); reader.close(); return returnStr; } catch (SocketTimeoutException e) { return ""; } catch (ConnectTimeoutException e) { return ""; } catch (Exception e) { return ""; } finally { HttpClientUtils.closeQuietly(httpResponse); HttpClientUtils.closeQuietly(httpClient); } } }
headers = { "Accept": "application/json", 'Content-type': "application/json" } data = { "code": code } print(token, code) response2 = requests.post('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' + token, headers=headers, data=data) print(response2.text) return ("hello word")
{"errcode":47001,"errmsg":"data format error hint: [ygdBJHnre-OoZ0Ca] rid: 624e7b64-5434b888-1ee593f6"}
postman body 用raw那个选中json格式并且加上请求头就可以了
关注后,可在微信内接收相应的重要提醒。
请使用微信扫描二维码关注 “微信开放社区” 公众号
代码已测,已通。关注WX公众号,回复 手机号 ,直接给java代码,还有更多的技术类干货等你哦~
token写到url后面,code用json
此接口需要付费使用吗?我每次使用需要验证码可是没能获取到验证码,是不是购买短信接口啥的
package servlet; import com.alibaba.fastjson.JSON; import net.sf.json.JSONObject; import org.apache.http.Consts; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.SocketTimeoutException; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Set; public class WXPhoneServlet2 extends HttpServlet { String js_code; PrintWriter out; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); js_code = request.getParameter("code"); out = response.getWriter(); String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=yourtooken"; Map<String, String> map = new HashMap<>(1); map.put("code", js_code); String res = WXPhoneServlet2.postJson(url, JSON.toJSONString(map), null); out.write("" + res); } public static String postJson(String url, String json, Map<String, String> headMap) { String returnStr; CloseableHttpClient httpClient = null; CloseableHttpResponse httpResponse = null; try { HttpPost post = new HttpPost(url); post.setConfig(RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build()); httpClient = HttpClientBuilder.create().build(); StringEntity s = new StringEntity(json, Consts.UTF_8); s.setContentType("application/json"); if (headMap != null && headMap.size() > 0) { Set<String> keySet = headMap.keySet(); for (String key : keySet) { post.addHeader(key, headMap.get(key)); } } post.setEntity(s); httpResponse = httpClient.execute(post); BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), StandardCharsets.UTF_8.name())); StringBuilder stringBuffer = new StringBuilder(100); String str; while ((str = reader.readLine()) != null) { stringBuffer.append(str); } returnStr = stringBuffer.toString(); reader.close(); return returnStr; } catch (SocketTimeoutException e) { return ""; } catch (ConnectTimeoutException e) { return ""; } catch (Exception e) { return ""; } finally { HttpClientUtils.closeQuietly(httpResponse); HttpClientUtils.closeQuietly(httpClient); } } }
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.apache.http.Consts;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class WXPhoneServlet2 extends HttpServlet {
String js_code;
PrintWriter out;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
js_code = request.getParameter("code");
out = response.getWriter();
String token = getTooken();
String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+token;
Map<String, String> map = new HashMap<>(1);
map.put("code", js_code);
String res = WXPhoneServlet2.postJson(url, JSON.toJSONString(map), null);
out.write("" + res);
out.close();
}
public static String getTooken(){
String url = "https://api.weixin.qq.com/cgi-bin/token";
String data = "grant_type=client_credential&appid=&secret=";
String access_token="";
try {
String s = new HttpUtils().SendHttpPOST(url, data);
JSONObject json1=JSONObject.fromObject(s);
Map<String, Object> map =json1;
for (Map.Entry<String, Object> entry : map.entrySet()) {
if(entry.getKey().equals("access_token")){
access_token = ""+entry.getValue();
}
}
} catch (Exception e) {
}
return access_token;
}
public static String postJson(String url, String json, Map<String, String> headMap) {
String returnStr;
CloseableHttpClient httpClient = null;
CloseableHttpResponse httpResponse = null;
try {
HttpPost post = new HttpPost(url);
post.setConfig(RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(10000).build());
httpClient = HttpClientBuilder.create().build();
StringEntity s = new StringEntity(json, Consts.UTF_8);
s.setContentType("application/json");
if (headMap != null && headMap.size() > 0) {
Set<String> keySet = headMap.keySet();
for (String key : keySet) {
post.addHeader(key, headMap.get(key));
}
}
post.setEntity(s);
httpResponse = httpClient.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),
StandardCharsets.UTF_8.name()));
StringBuilder stringBuffer = new StringBuilder(100);
String str;
while ((str = reader.readLine()) != null) {
stringBuffer.append(str);
}
returnStr = stringBuffer.toString();
reader.close();
return returnStr;
} catch (SocketTimeoutException e) {
return "";
} catch (ConnectTimeoutException e) {
return "";
} catch (Exception e) {
return "";
} finally {
HttpClientUtils.closeQuietly(httpResponse);
HttpClientUtils.closeQuietly(httpClient);
}
}
}
headers = { "Accept": "application/json", 'Content-type': "application/json" } data = { "code": code } print(token, code) response2 = requests.post('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' + token, headers=headers, data=data) print(response2.text) return ("hello word")
{"errcode":47001,"errmsg":"data format error hint: [ygdBJHnre-OoZ0Ca] rid: 624e7b64-5434b888-1ee593f6"}
postman body 用raw那个选中json格式并且加上请求头就可以了