微信授权登录

简介: 今天在这里给大家介绍下微信授权登录主要分一下几个步骤喜欢的朋友可以关注下,粉丝也缺。1、引导用户进入授权页面同意授权,获取code 2、通过code换取网页授权access_token(与基础支持中的access_t...

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

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

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

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


相关文章
|
22天前
|
小程序 安全 数据安全/隐私保护
微信小程序全栈开发中的身份认证与授权机制
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的身份认证与授权机制。身份认证包括手机号验证、微信登录和第三方登录,而授权机制涉及角色权限控制、ACL和OAuth 2.0。实践中,开发者可利用微信登录获取用户信息,集成第三方登录,以及实施角色和ACL进行权限控制。注意点包括安全性、用户体验和合规性,以保障小程序的安全运行和良好体验。通过这些方法,开发者能有效掌握小程序全栈开发技术。
|
1月前
|
小程序 API
微信小程序——授权登录
微信小程序——授权登录
18 0
|
4月前
|
存储 JavaScript 开发工具
uniapp-实现微信授权登录
uniapp-实现微信授权登录
765 0
|
4月前
|
存储 JSON JavaScript
前后端分离项目知识汇总(微信扫码登录,手机验证码登录,JWT)-1
前后端分离项目知识汇总(微信扫码登录,手机验证码登录,JWT)
67 0
|
26天前
如何在PC端登录多个微信号?怎么操作免费多开电脑版微信?
如何在PC端登录多个微信号?怎么操作免费多开电脑版微信?
|
2月前
|
JSON 小程序 C#
微信网页授权之使用完整服务解决方案
微信网页授权之使用完整服务解决方案
|
3月前
|
小程序 JavaScript
微信小程序授权登录?
微信小程序授权登录?
|
4月前
|
JSON 前端开发 安全
前后端分离项目知识汇总(微信扫码登录,手机验证码登录,JWT)-2
前后端分离项目知识汇总(微信扫码登录,手机验证码登录,JWT)
58 0
|
4月前
|
小程序 数据安全/隐私保护
微信小程序实现一个简单的登录功能
微信小程序实现一个简单的登录功能
|
4月前
|
小程序
uniapp 微信小程序登录 新手专用 引入即可
uniapp 微信小程序登录 新手专用 引入即可
32 0

热门文章

最新文章