java 获得 微信 UserId
....
public String cs() throws Exception{ /*访问页面,服务器会得到 code(request.getParameter("code")) ,根据 code 换取网页授权access_token 根据获取的access_token,openid 然后再请求获取userinfo的接口。得到微信用户的所有信息*/ String code = request().getParameter("code"); String scope = request().getParameter("scope"); JSONObject jsonObject=new JSONObject(); if(null != code && !"".equals(code)){ jsonObject = UserInfo.getOAuthOpenId("企业ID","应用秘钥",code); //jsonObject.put("UserId","kaitou_kid"); if(!jsonObject.equals("")){ String sql = "select * from T_yh where USERID='"+jsonObject.get("UserId").toString()+"'"; dBSelect(sql); } jsonObject.put("existence","1"); // 存在 jsonObject.put("status","1"); // 成功 }else{ jsonObject.put("status","-1"); System.out.println("============== 失败!"); } return jsonObject.toString(); }
...
import java.util.Map; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import net.sf.json.JSONException; import net.sf.json.JSONObject; import weixin.qq.weixin.tool.MyX509TrustManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import net.sf.json.JSONObject; public class UserInfo { //文档:https://work.weixin.qq.com/api/doc/90000/90135/91022 //获得凭证 corpid:企业ID,corpsecret:应用的凭证密钥 public static String voucher = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET"; //该接口用于根据code获取成员信息 //access_token:调用接口凭证(在上一个链接内获取的数据中), // code:通过成员授权获取到的code,最大为512字节。每次成员授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。 public static String Member_information = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE"; //使用user_ticket获取成员详情 public static String userinfo_url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserdetail?access_token=ACCESS_TOKEN"; /*access_token:调用接口凭证, * code:通过成员授权获取到的code,每次成员授权带上的code将不一样*/ /** * 网页授权,根据code取得openId * @param appid 公众号的唯一标识 * @param secret 公众号的appsecret密钥 * @param code code为换取access_token的票据 * @return */ public static JSONObject getOAuthOpenId(String appid, String secret, String code ) { //根据code获取成员信息 JSONObject jsonObject=null; String voucherStr = voucher.replace("ID", appid).replace("SECRET", secret); JSONObject voucherJsonObject = JSONObject.fromObject(httpRequest(voucherStr, "GET", null)); /*返回一下json格式数据 { "UserId":"USERID", // 成员UserID "DeviceId":"DEVICEID" // 手机设备号(由微信在安装时随机生成,删除重装会改变 "user_ticket": "USER_TICKET", //成员票据,最大为512字节 "expires_in":7200}*/ //user_token的有效时间(秒) // 如果请求成功 if (null != voucherJsonObject) { try { String MemberInformationStr = Member_information.replace("ACCESS_TOKEN", voucherJsonObject.get("access_token").toString()) .replace("CODE", code); JSONObject MemberInformationJsonObject = JSONObject.fromObject(httpRequest(MemberInformationStr, "GET", null)); /*{ "errcode": 0, "errmsg": "ok", "UserId":"USERID", "DeviceId":"DEVICEID"}*/ if (null != MemberInformationJsonObject) { jsonObject=MemberInformationJsonObject; } } catch (Exception e) { jsonObject = null; // 获取token失败 System.out.println("网页授权获取openId失败 errcode:{} errmsg:{}"); } } return jsonObject; } /** * 发起https请求并获取结果 * * @param requestUrl 请求地址 * @param requestMethod 请求方式(GET、POST) * @param outputStr 提交的数据 */ public static String httpRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject = null; //请求的url URL url = null; //请求的输入流 BufferedReader in = null; //输入流的缓冲 StringBuffer sb = new StringBuffer(); try{ url = new URL(requestUrl); in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8") ); String str = null; //一行一行进行读入 while((str = in.readLine()) != null) { sb.append( str ); } } catch (Exception ex) { } finally{ try{ if(in!=null) { in.close(); //关闭流 } }catch(IOException ex) { } } System.out.println("==============httpRequest方法结束==============="); return sb.toString(); } public static void main(String[] args) { // TODO Auto-generated method stub } }
时刻告诉自己,自己是个菜鸡......