微信授权登录

今天在这里给大家介绍下微信授权登录主要分一下几个步骤

喜欢的朋友可以关注下,粉丝也缺。

1、引导用户进入授权页面同意授权,获取code 

2、通过code换取网页授权access_token(与基础支持中的access_token不同) 

3、如果需要,开发者可以刷新网页授权access_token,避免过期 

4、通过网页授权access_token和openid获取用户基本信息(支持UnionID机制) 

下面就来具体给大家介绍怎么实现

//得到code
public final static String code_url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";
//得到openid
public final static String open_id_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
//刷新access_token
public final static String access_token_url="https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
//获取用户信息
public final static String user_info_url="https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
//判断用户是否关注公众号
public final static String user_guanzhu="https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

String appid=WeiXinUitls.appid;//公众号ID
String code = request.getParameter("code");

String appsecret = WeiXinUitls.appsecret;//公众号的appsecret

String grant_type = "authorization_code";

String returnJSON = HttpTool.getToken(appid, appsecret, grant_type,code);
JSONObject obj = JSONObject.fromObject(returnJSON);

if(!(obj==null)){

String openid = obj.get("openid").toString();
String token = obj.get("access_token").toString();
String retoken = obj.get("refresh_token").toString();

这里就已经成功获取微信用户的openid

}

public static String getToken(String appid,String appsecret, String grantType, String code){
String requestUrl = open_id_url.replace("APPID", appid).replace("SECRET", appsecret).replace("CODE", code).replace("authorization_code",grantType);
JSONObject jsonObject = httpRequest(requestUrl,"GET", null);
//System.out.println("得到json的大�?+jsonObject.size());
return jsonObject.toString();
}



String appid=WeiXinUitls.appid;
String redirect_url="此处为返回code的servlet(注意的是这里的url需要经过编码)";
HttpTool.getCode(appid, redirect_url);

public static String getCode(String appid,String redirect_uri){
String requestUrl = code_url.replace("APPID", appid).replace("REDIRECT_URI", redirect_uri).replace("SCOPE", "snsapi_userinfo").replace("STATE", "wxsq123");
JSONObject jsonObject = httpRequest(requestUrl,"GET", null);
return jsonObject.toString();
}

public static JSONObject httpRequest(String requestUrl,
String requestMethod, String outputStr) {
JSONObject jsonObject = null;
StringBuffer buffer = new StringBuffer();
try {
// /1、解决https请求的问�?


// 创建SSLContext对象,并使用我们指定的信任管理器初始�?
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();


URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url
.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);


httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);


// /2、兼容GET、POST两种方式


// 设置请求方式(GET/POST�?
httpUrlConn.setRequestMethod(requestMethod);


if ("GET".equalsIgnoreCase(requestMethod)) {
httpUrlConn.connect();
}


// /3、兼容有数据提交、无数据提交两种情况


// 当有数据�?��提交�?
if (null != outputStr) {
OutputStream outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱�?
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}


// 将返回的输入流转换成字符�?
InputStream inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(
inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);


String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
bufferedReader.close();
inputStreamReader.close();
// 释放资源
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
jsonObject = JSONObject.fromObject(buffer.toString());
} catch (ConnectException ce) {
} catch (Exception e) {
}
return jsonObject;
}

 

 

如遇到问题欢迎进群308742428

 

 

 

喜欢的朋友可以关注下,粉丝也缺。

如果对你有帮助,请打赏一下谢谢。

你可能感兴趣的:(微信,授权登录,openid,微信开发,攻城狮的日常)