WeChat, Weibo, QQ, these are the most commonly used mobile APPs nowadays. How can we make products without being related to them? I don’t think anyone wants many accounts and passwords for login, so I can’t remember them at all!
In order to increase the user experience, users can quickly register and log in, and the need for third-party accounts to log in is born.
1. WeChat
1) WeChat login is also the most tricky thing. It takes 300 oceans to become a developer account. There is no way for anyone to make the WeChat user base large, so I had to admit it. Then, you need to create a website application when logging into the backend of the website, fill in the authorization callback domain (the domain name of the login website) and just fill in the domain name.
Application address: https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN
2) Check the interface provided by WeChat and write java code
public void wx() { try { response.sendRedirect("https://open.weixin.qq.com/connect/qrconnect?appid=" + ShareLoginDict.WEIXINKEY.getState() + "&redirect_uri=" + URLEncoder.encode(ShareLoginDict.WEIXINURL.getState()) + "&response_type=code&scope=snsapi_login&state=66666#wechat_redirect"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }@Override public Result userwx(String return_code) { Result result = new Result(); Map<String, Object> token = (Map<String, Object>) WeiXinAPI .getToken(return_code); if (token != null && token.get("access_token") != null) { Map<String, Object> user = (Map<String, Object>) WeiXinAPI .getWxUser(token.get("access_token").toString(), token.get("openid").toString()); if (user != null) { result.addModel("openid", user.get("openid")); result.addModel("nickname", user.get("nickname")); result.addModel("headimgurl", user.get("headimgurl")); result.addModel("data", "data_success"); }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } return result; }When the user logs in through WeChat, call the WeChat interface to obtain the user interface and return to the openid, nickname, and avatar of the WeChat terminal; then store this information into the browser's cookie. When the user browses other information, distinguish if the user is logged in with WeChat, the interceptor directly obtains the user's information from the cookie and displays the nickname and avatar, and at the same time determines whether the openid is bound to the user in the database. If there is no binding, the user prompts the user to register.
2. QQ login
1) QQ Internet Creation Application
Before accessing QQ login, the website needs to apply first to obtain the corresponding appid and appkey to ensure that the website and users can be correctly verified and authorized in the subsequent process.
Uses for appid and appkey
appid: The unique identifier of the application. During the OAuth2.0 authentication process, the appid value is the value of oauth_consumer_key.
appkey: The key corresponding to appid, used to verify the legitimacy of the application when accessing user resources. During the OAuth2.0 authentication process, the appkey value is the value of oauth_consumer_secret.
Application address: http://connect.qq.com/intro/login/
2) Check the interface provided by QQ and write java code
public void qq() { try { response.sendRedirect("https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=" + ShareLoginDict.QQKEY.getState() + "&redirect_uri=" + ShareLoginDict.QQURL.getState() + "&scope=get_user_info"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }@Override public Result userqq(String return_code) { Result result = new Result(); Map<String, Object> token = (Map<String, Object>) QQAPI .getToken(return_code); if (token != null && token.get("access_token") != null) { Map<String, Object> tokenme = (Map<String, Object>) QQAPI .getTokenMeOpenId(token.get("access_token").toString()); if (tokenme != null && tokenme.get("openid") != null) { Map<String, Object> user = (Map<String, Object>) QQAPI .getQqUser(token.get("access_token").toString(), tokenme.get("openid").toString()); if (user != null) { result.addModel("openid", tokenme.get("openid")); result.addModel("nickname", user.get("nickname")); result.addModel("figureurl", user.get("figureurl")); result.addModel("data", "data_success"); }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } return result; }When the user logs in through QQ, call the QQ interface to obtain the user interface to return the openid, nickname, and avatar of the QQ side; then store this information into the browser's cookie. When the user browses other information, distinguish if the user is logged in with WeChat, the interceptor directly obtains the user's information from the cookie and displays the nickname and avatar, and at the same time determines whether the openid is bound to the user in the database. If there is no binding, prompt the user to register.
3 Weibo login
1) Create an application on Weibo
Application address: http://open.weibo.com/authentication
2) Check the interface provided by Weibo and write java code
public void wb() { try { response.sendRedirect("https://api.weibo.com/oauth2/authorize?client_id=" + ShareLoginDict.WEIBOKEY.getState() + "&redirect_uri=" + ShareLoginDict.WEIBOURL.getState() + "&response_type=code"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }@Override public Result userwb(String return_url, String return_code) { Result result = new Result(); Map<String, Object> token = (Map<String, Object>) WeiBoAPI.getToken( return_url, return_code); if (token != null && token.get("access_token") != null) { Map<String, Object> user = (Map<String, Object>) WeiBoAPI .getWbUser(token.get("access_token").toString(), token.get("uid").toString()); if (user != null) { result.addModel("name", user.get("screen_name")); result.addModel("pic", user.get("avatar_large")); result.addModel("idstr", user.get("idstr")); result.addModel("data", "data_success"); }else{ result.addModel("data", "data_null"); } }else{ result.addModel("data", "data_null"); } return result; }When the user logs in through Weibo, call the Weibo interface to obtain the user interface and return to the idstr, nickname, and avatar on the Weibo side; then store this information into the browser's cookie. When the user browses other information, distinguish if the user is logged in with WeChat, the interceptor directly obtains the user's information from the cookie and displays the nickname and avatar, and at the same time determines whether the openid is bound to the user in the database. If there is no binding, the user prompts the user to register.
Thank you for reading, I hope it can help you. Thank you for your support for this site!