第三方
WEB开发时常会涉及到第三方登录的情况,刚刚做了第三方登录,为避免忘记做个记录
环境:windows + jdk1.7 + tomcat1.7
框架:SpringMVC + JPA
准备:
- 申请
- 配置文件
- API
申请成为开发者
1.新浪:
(1).html及头部
<html xmlns:wb="https://open.weibo.com/wb> //html中添加此域
<meta property="wb:webmaster" content="e07c114e01c43d01"/> //申请测试站时,获取
(2).配置文件, client_ID和client_SERCRET申请开发站点时获得
client_ID = x
client_SERCRET = x
redirect_URI = http://www.hejzj.com/sina/loginSinaAction.do
baseURL=https://api.weibo.com/2/
accessTokenURL=https://api.weibo.com/oauth2/access_token
authorizeURL=https://api.weibo.com/oauth2/authorize
rmURL=https://rm.api.weibo.com/2/
(3).官方下载开发包
//将使用的开发包出去test部分内容,其余打成jar包。 放入maven仓库(也可直接引入)
weibo_4j
2.微信:
(1).申请较为繁琐,等待时间长。(个人认证未通过,企业认证可以。 需要提交申请,以及开发者资质申请)
(2).无配置文件
(3).无官方开发包,参照API开发
3.QQ
(1).申请较为繁琐(个人认证未通过,企业认证需提交申请)
(2)配置文件(app_ID和app_KEY申请时,获得)
app_ID = x
app_KEY = x
redirect_URI = http://www.hejzj.com/QQ/afterlogin.do
scope = get_user_info //权限域,有多种设置,目前第三方登录使用这个足够
baseURL = https://graph.qq.com/
getUserInfoURL = https://graph.qq.com/user/get_user_info
accessTokenURL = https://graph.qq.com/oauth2.0/token
authorizeURL = https://graph.qq.com/oauth2.0/authorize
getOpenIDURL = https://graph.qq.com/oauth2.0/me
addTopicURL = https://graph.qq.com/shuoshuo/add_topic
addBlogURL = https://graph.qq.com/blog/add_one_blog
addAlbumURL = https://graph.qq.com/photo/add_album
uploadPicURL = https://graph.qq.com/photo/upload_pic
listAlbumURL = https://graph.qq.com/photo/list_album
addShareURL = https://graph.qq.com/share/add_share
checkPageFansURL = https://graph.qq.com/user/check_page_fans
addTURL = https://graph.qq.com/t/add_t
addPicTURL = https://graph.qq.com/t/add_pic_t
delTURL = https://graph.qq.com/t/del_t
getWeiboUserInfoURL = https://graph.qq.com/user/get_info
getWeiboOtherUserInfoURL = https://graph.qq.com/user/get_other_info
getFansListURL = https://graph.qq.com/relation/get_fanslist
getIdolsListURL = https://graph.qq.com/relation/get_idollist
addIdolURL = https://graph.qq.com/relation/add_idol
delIdolURL = https://graph.qq.com/relation/del_idol
getTenpayAddrURL = https://graph.qq.com/cft_info/get_tenpay_addr
getRepostListURL = https://graph.qq.com/t/get_repost_list
version = 2.0.0.0
(3)开发包:可下载SDK
回调地址
1.新浪
在网站控制台里面,设置回调地址在配置文件中:redirect_URI
2.微信
回调地址只需要在申请地址的域名之下,没有固定写法
3.QQ
回调地址在配置文件中:redirect_URI
流程
1.新浪
具体说明请查看API
//请求第三方
Oauth oauth = new Oauth();
String url = oauth.authorize("code", ""); //固定传入code
return "redirect:" + url; //url回调
//回调处理
Oauth oauth = new Oauth();
AccessToken accessToken = oauth.getAccessTokenByCode(code);
String access_token = accessToken.getAccessToken(); //获取token
String uid = accessToken.getUserUid(); //新浪用户唯一标识
Users users = new Users(access_token);
User user = users.showUserById(uid); //获取用户信息
2.微信
//连接接口
public static String baseLogin = "https://open.weixin.qq.com/connect/qrconnect?";
//oauth2获取access_token接口
public static String baseToken = "https://api.weixin.qq.com/sns/oauth2/access_token?";
//获取用户信息接口
public static String baseUser = "https://api.weixin.qq.com/sns/userinfo?";
//appId和appSecret申请时获得
public static String appId = "x";
public static String appSecret = "x";
//回调地址
public static String redirect_uri = "http://www.hejzj.com/weixin/afterlogin.do";
//固定
public static String response_type = "code";
//权限
public static String scope = "snsapi_login";
//固定
public static String grant_type = "authorization_code";
//调用第三方
String url = baseLogin + "appid=" + appId + "&redirect_uri=" + redirect_uri +
"&response_type=" + response_type + "&scope=" + scope;
//通过httpClient方法获取httpEntity
HttpGet get = new HttpGet(url);
CloseableHttpResponse httpResponse = null;
httpResponse = httpClient.execute(get);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = httpResponse.getEntity(); //封装了返回的内容
//转换成jsonobject
JSONObject jsonObject = JSONObject.fromObject(EntityUtils.toString(entity));
}
//唯一标识
String openId = jsonObject.getString("openid");
//用户ID
String unionid = jsonObject.getString("unionid");
//获取用户信息接口,再次使用httpClient
String userUrl = baseUser + "access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN";
get.setURI(new URI(userUrl));
httpResponse = httpClient.execute(get);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity entity2 = httpResponse.getEntity();
//此时返回的JsonObject封装了用户的信息
JSONObject jsonObject2 = JSONObject.fromObject(EntityUtils.toString(entity2));
}
3.QQ
//调用第三方
response.sendRedirect(new Oauth().getAuthorizeURL(request));
//回调
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
//accessToken
accessToken = accessTokenObj.getAccessToken();
//过期时间
tokenExpireIn = accessTokenObj.getExpireIn();
OpenID openIDObj = new OpenID(accessToken);
//唯一标识
openID = openIDObj.getUserOpenID();
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
//用户信息对象
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
其他逻辑属于业务逻辑