准备工作
http://open.weibo.com/index.php
注册账号 完善信息 认证等
创建网页应用
最好有个花生壳弄个域名方便测试我是这么玩的。
应用创建好后,填好授权回调url 比如我的
http://maobo.uicp.cn/xxx/WeiBoOauth
官方文档
http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6
没什么卵用 了解一下
实例demo参考此篇
http://blog.csdn.net/u013219600/article/details/25658795
sdk 下载
http://open.weibo.com/wiki/SDK?sudaref=blog.csdn.net&display=0&retcode=6102#Java_SDK
解压后导入eclipse中,注意遇到的坑
config.properties配置文件里面配好了
client_ID=20096xx42
client_SERCRET=046818a939dxx671744e3bb6a
redirect_URI=http://maobo.uicp.cn/xx/WeiBoOauth
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/
这里main方法运行出来的url 大致为:
https://api.weibo.com/oauth2/authorize?client_id=200xx4442&redirect_uri=http://maobo.uicp.cn/xx/WeiBoOauth&response_type=code
这个client_id是错的,须改成网站上申请的id 否则一直报错。url重定向错误。
改完之后就正常进入了
服务器端
将weibo4j整个导入项目。
servlet方法
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("微博回调页面");
String code = request.getParameter("code");
try {
Oauth oauth = new Oauth();
String token = oauth.getAccessTokenByCode(code).toString();
System.out.println(token);
String str[] = token.split(",");
String accessToken = str[0].split("=")[1];
System.out.println("accessToken:"+accessToken);
String str1[] = str[3].split("]");
String uid = str1[0].split("=")[1];
System.out.println("uid:"+uid);
Users um = new Users(accessToken);
User user = um.showUserById(uid);
// 通过user的get方法可以获得其属性,具体属性可按ctrl点击User类,进入User类查看
System.out.println("hehe :" + user.toString());
RequestDispatcher requestDispatcher = request.getRequestDispatcher("weibo.jsp");
requestDispatcher.forward(request, response);
} catch (WeiboException e) {
e.printStackTrace();
}
}