新浪微博授权登录

准备工作
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();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值