微信静默授权获取微信用户信息,获取基础token, 获取ticket用户定位和微信分享集合

通过微信appid和secret 获取 openid

 import org.weixin4j.http.HttpsClient;//引入微信的包
 import org.weixin4j.http.Response;//微信
 import com.alibaba.fastjson.JSONObject;//阿里巴巴
 private JSONObject getWeChantInfoByAppidAndCode(String code) {
        try {
         String appid="123456";
         String secret="123456";
            //拼接参数
            String param = "?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
            //创建请求对象
            HttpsClient http = new HttpsClient();
            //调用获取access_token接口
            //org.weixin4j.http.Response
            Response res = http.get("https://api.weixin.qq.com/sns/oauth2/access_token" + param);
            //根据请求结果判定,是否验证成功
            //com.alibaba.fastjson.JSONObject
            JSONObject jsonObj = res.asJSONObject();
            log.error("通过appid和秘钥code获取微信返回信息=========================" + jsonObj);
             if (jsonObj != null) {
              Object errcode = jsonObj.get("errcode");
                if (errcode != null) {
                    //返回异常信息
                    System.out.println("getOpenid()微信返回异常信息" + errcode);
                }
                //获取到openid
                Sring openId = jsonObj.getString("openid");
             }
            return jsonObj;
        } catch (Exception e) {
            log.error(e.getMessage());
            log.error("异常捕获----通过appid和秘钥code获取微信返回信息");
            return null;
        }

    }

根据appid和secret 获取基础 access_token 在这里插入代码片

import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class AccessTokenUtil {
    /**
     * 获取accessToken
     *
     * @param appID    微信公众号凭证
     * @param appScret 微信公众号凭证秘钥
     * @return
     */
    public static AccessToken getAccessToken(String appID, String appScret)  throws  Exception{
        // 访问微信服务器
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appID + "&secret="
                + appScret;
        try {
            URL getUrl = new URL(url);
            HttpURLConnection http = (HttpURLConnection) getUrl.openConnection();
            http.setRequestMethod("GET");
            http.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);

            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] b = new byte[size];
            is.read(b);

            String message = new String(b, "UTF-8");
            JSONObject json = JSONObject.fromObject(message);
          //获取基础token
          String token1=(json.getString("access_token"));
          //获取过期时间
          Integer Expires_in=(new Integer(json.getString("expires_in")));
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return token;
    }

}

//根据基础token获取ticket

import net.sf.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
private static final String ACCESS_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=TOKEN&type=jsapi";

 public static String getAccessTicket(String token){
       
        String url = ACCESS_TICKET_URL.replace("TOKEN",token);
        JSONObject jsonObject = doGet(url);
        String ticket="";
        if(jsonObject != null){
            String ticket=(jsonObject.getString("ticket"));
        }
        return ticket;
    }

 public  static  JSONObject doGet(String url){
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        JSONObject jsonObject = null;
        try {
            HttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            if(entity != null){
                String result = EntityUtils.toString(entity, "UTF-8");
                jsonObject = JSONObject.fromObject(result);

            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

你可能感兴趣的:(微信静默授权获取微信用户信息,获取基础token, 获取ticket用户定位和微信分享集合)