QQ,微博与微信的接口(崩溃啊!!!上一次写了一大半结果死机,死机···死机了·······)

时间:2016-12-18 02:12:13   收藏:0   阅读:439

突然就不想写了,尼玛写了一半结果没了,好心塞好难受····从头再来吧!

先放三个连接:

https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN;

https://connect.qq.com/index.html;

http://open.weibo.com/webmaster

QQ和微博是免费的,微信是300,目前是这样,以后谁知道呢?!

我这边全部下载的java.sdk,具体的架包可以去上面的官网下载,我的项目配置是maven

 

先来QQ

<!--qq登陆 -->
<dependency>
<groupId>com.qq</groupId>
<artifactId>qq-sdk4J</artifactId>
<version>2.0</version>
</dependency>

架包配置好以后准备界面

<a href="http://diguagua.net/user/qq/login/">
<img src="static/images/login/qq.png" alt="QQ授权登录 QQ授权登录开发 QQ授权登录管理">
</a>

页面的按钮是自定义配置,官网给的图标太丑没用,页面布置好以后开始写后台@RequestMapping("/qq")

@Login(action = Action.Skip)//自定义注解,直接放行
@RequestMapping("/login")
public ModelAndView qqLogin() {
String url = null;
try {
url = new Oauth().getAuthorizeURL(request);
} catch (QQConnectException e) {
log.error("qq login exception:" + e.getMessage());
}
return sendRedirect(url);
}
@Login(action = Action.Skip)
@RequestMapping("/afterlogin")
public ModelAndView afterLogin(ModelAndView model) {
try {
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
if ("".equals(accessTokenObj.getAccessToken())) {
return sendRedirect(null);
} else {
String accessToken = accessTokenObj.getAccessToken();
OpenID openIDObj = new OpenID(accessToken);
String openId = openIDObj.getUserOpenID();
model.setViewName("/userCenter");
return model;
}
@Login(action = Action.Skip)
@RequestMapping("/qqLogin")
@ResponseBody
public Map<String, String> qqLogin(@RequestParam("openId") String openId) {
Map<String, String> result = new HashMap<>();
boolean flag = loginService.isHaveUserName(openId);
String retUrl = SSOHelper.getRetUrl(request);
if (StringUtils.isNotEmpty(retUrl)) {
retUrl = HttpUtil.decodeURL(retUrl);
SSOHelper.cleanRetUrl(request, response);
} else {
retUrl = "center";
}
return result;
}
private void createCookie(String userId) {
Token token = new SSOToken(request);
token.setUid(userId);
SSOHelper.setSSOCookie(request, response, token, false);
}

登录用java写必须用页面跳转下,这样能把cookie带过去下次登录根据cookie直接登录,上面的代码我删除了一点,基本逻辑就是这样,根据回调地址拿到唯一凭证,然后在网页做自己的逻辑处理

 

 

 

再来微博

<a href="http://diguagua.net/user/weibo/login/" class="mid">
<img src="static/images/login/weibo.png">
</a>

后台

@Controller
@RequestMapping("/weibo")
public class WeiboLoginController extends BaseController {

@Autowired
private LoginService loginService;



@Login(action = Action.Skip)
@RequestMapping("/login")
public ModelAndView weiboLogin() {
String url = null;
try {
Oauth oauth = new Oauth();
url = oauth.authorize("code");
} catch (WeiboException e) {
log.error("weibo login exception:" + e.getMessage());
}
return sendRedirect(url);
}

@Login(action = Action.Skip)
@RequestMapping("/afterlogin")
public ModelAndView afterLogin(ModelAndView model){
String code = request.getParameter("code");
String token;
try {
Oauth oauth = new Oauth();
token = oauth.getAccessTokenByCode(code).getAccessToken();
Account am = new Account(token);
String openId = am.getUid().getString("uid");
} catch (WeiboException e) {
log.error("weibo after login exception:" + e.getMessage());
} catch (JSONException e) {
log.error("weibo after login JSONException:" + e.getMessage());
}
model.setViewName("/userCenter");
return model;
}

 

 

 

最后微信

基本上是大同小异,但是从API上看还是QQ的要比微博的好,微博的要比微信好,其实我觉得阿里的最好,之前调用过阿里的一个接口,觉得不错!

 

 

最后是微信,微信的我不想吐槽,首先第一他的回调地址和官网授权是一样的,不能改!!!!!他么的不能改!!!!截至到目前为止还没有解决···所以很尴尬虽然步骤什么的大同小异,并且微信还收了钱!!!星期一再来写,哦哈哈哈哈~~~

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!