新浪微博客户端开发主要流程
0、在http://open.weibo.com/ 上面申请帐号,主要是获得一个到AppKey和AppSecret
1、使应用redirect到https://api.weibo.com/oauth2/authorize,并附带参数:client_id=2671843862&redirect_uri=http://www.baidu.com&response_type=code
参数中的client_id就是0中的AppKey,redirect_uri为注册时填写的回调地址
2、用户在2中的页面填写了正确的用户名和密码,并且单击“同意”后,sina微博会redirect到http://www.baidu.com?code=d882fc2c3dfc27f76ccd64030100ea8f
3、客户端应用在后台通过httpclient请求https://api.weibo.com/oauth2/access_token,并携带参数client_id(AppKey)、client_secret(AppSecret)、grant_type(固定为authorization_code)、redirect_uri(http://www.baidu.com),sina微博返回数据:{"access_token":"2.00vJ7xOBqdmouC39149f2344suQQIB","remind_in":"634369","expires_in":634369,"uid":"1137163447"}
4、在第三步中获取到access_token和uid后,以后就可以利用这写信息去获得用户的数据,例如获得用户的基础信息,可以通过在应用后台通过httpclient请求url:https://api.weibo.com/2/users/show.json?uid=1137163447,并携带参数uid,及设置请求头:
headers.add(new Header("Authorization", "OAuth2 " + token)); headers.add(new Header("API-RemoteIP", ipaddr.getHostAddress())); client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
这样,sina微博会返回以下信息:
{ "id": 1137163447, "idstr": "1137163447", "screen_name": "_杨风涛_", "name": "_杨风涛_", "province": "11", "city": "1000", "location": "北京", "description": "一个正在奋斗的IT男", "url": "", "profile_image_url": "http://tp4.sinaimg.cn/1137163447/50/5635661600/1", "profile_url": "yangfengtao", "domain": "yangfengtao", "weihao": "", "gender": "m", "followers_count": 16, "friends_count": 29, "statuses_count": 35, "favourites_count": 0, "created_at": "Wed Feb 16 11:38:19 +0800 2011", "following": false, "allow_all_act_msg": false, "geo_enabled": true, "verified": true, "verified_type": 0, "remark": "", "status": { "created_at": "Wed Sep 26 21:43:20 +0800 2012", "id": 3494625061135036, "mid": "3494625061135036", "idstr": "3494625061135036", "text": "test", "source": "<a href=\"http://binary.duapp.com/?p=244\" rel=\"nofollow\">牵着蜗牛去散步</a>", "favorited": false, "truncated": false, "in_reply_to_status_id": "", "in_reply_to_user_id": "", "in_reply_to_screen_name": "", "geo": null, "reposts_count": 0, "comments_count": 0, "attitudes_count": 0, "mlevel": 0, "visible": { "type": 0, "list_id": 0 } }, "allow_all_comment": true, "avatar_large": "http://tp4.sinaimg.cn/1137163447/180/5635661600/1", "verified_reason": "趣拿软件科技有限公司工程师杨风涛", "follow_me": false, "online_status": 1, "bi_followers_count": 3, "lang": "zh-cn" }
然后我们可以从中提取我们想要的信息了。
5,也可以发表微博,https://api.weibo.com/2/statuses/repost.json,携带参数id(用户id)、status(发表的微博内容)、is_comment(是否在转发的同时发表评论,0:否、1:评论给当前微博、2:评论给原微博、3:都评论,默认为0),及设置请求头:
1 headers.add(new Header("Authorization", "OAuth2 " + token)); 2 headers.add(new Header("API-RemoteIP", ipaddr.getHostAddress())); 3 client.getHostConfiguration().getParams() .setParameter("http.default-headers", headers);
这样,sina微博会返回以下信息:
{ "created_at": "Wed Sep 26 21:59:52 +0800 2012", "id": 3494629217755329, "mid": "3494629217755329", "idstr": "3494629217755329", "text": "yft", "source": "<a href=\"http://binary.duapp.com/?p=244\" rel=\"nofollow\">牵着蜗牛去散步</a>", "favorited": false, "truncated": false, "in_reply_to_status_id": "", "in_reply_to_user_id": "", "in_reply_to_screen_name": "", "geo": null, "user": { "id": 1137163447, "idstr": "1137163447", "screen_name": "_杨风涛_", "name": "_杨风涛_", "province": "11", "city": "1000", "location": "北京", "description": "一个正在奋斗的IT男", "url": "", "profile_image_url": "http://tp4.sinaimg.cn/1137163447/50/5635661600/1", "profile_url": "yangfengtao", "domain": "yangfengtao", "weihao": "", "gender": "m", "followers_count": 16, "friends_count": 29, "statuses_count": 34, "favourites_count": 0, "created_at": "Wed Feb 16 11:38:19 +0800 2011", "following": false, "allow_all_act_msg": false, "geo_enabled": true, "verified": true, "verified_type": 0, "allow_all_comment": true, "avatar_large": "http://tp4.sinaimg.cn/1137163447/180/5635661600/1", "verified_reason": "趣拿软件科技有限公司工程师杨风涛", "follow_me": false, "online_status": 0, "bi_followers_count": 3, "lang": "zh-cn" }, "reposts_count": 0, "comments_count": 0, "attitudes_count": 0, "mlevel": 0, "visible": { "type": 0, "list_id": 0 } }
6、其他功能请参考官方文档
浙公网安备 33010602011771号