网上有很多Oauth1.0的封装,但没Oauth2.0的,故我就封装了2.0的给大家分享
weibo5j这个是个自己修改的,因为项目中有1.o的包,冲突了
package com.tuan.web.service.weibo;
import org.springframework.stereotype.Service;
import com.tuan.web.service.weibo.util.WeiboUtils;
import weibo5j.Account;
import weibo5j.Friendships;
import weibo5j.Oauth;
import weibo5j.Timeline;
import weibo5j.Users;
import weibo5j.Weibo;
import weibo5j.http.AccessToken;
import weibo5j.model.User;
import weibo5j.model.UserWapper;
import weibo5j.model.WeiboException;
import weibo5j.org.json.JSONObject;
/**
* @Title: SinaWeiboV2.java
* @Package com.tuan.web.service.weibo
* @Description: TODO(新浪围脖)
* @author longzhun
* @date 2012-7-27 下午03:01:23
* @version V2.0
*/
@Service
public class SinaWeiboV2 {
private Weibo weibo;
private Oauth oauth;
public SinaWeiboV2(){
oauth = new Oauth();
weibo = new Weibo();
}
/**
* 设置accesstoken
*/
public void setToken(String code) throws WeiboException{
AccessToken token = oauth.getAccessTokenByCode(code);
weibo.setToken(token.getAccessToken());
}
public String getRequestToken() throws WeiboException{
return oauth.authorize("code");
}
/**
* 根据用户id获取用户信息
*/
public User getUserInfo(String uid) throws WeiboException{
Users user = new Users();
User userInfo = user.showUserById(uid);
return userInfo;
}
/**
* 获取用户Id
*/
public String getUserId() throws Exception{
Account am = new Account();
JSONObject json = am.getUid();
if(json == null){
return "";
}
return json.getString("uid");
}
/**
* 添加关注
*/
public void createFriend(String uid) throws WeiboException{
Friendships fm = new Friendships();
UserWapper friend= fm.getFollowersById(uid);
if(friend == null){
fm.createFriendshipsById(uid);
}
}
/**
* 发布围脖
*/
public void publicMsg(String status,String fileName) throws Exception{
Timeline tm = new Timeline();
if (fileName == null || "".equals(fileName)){
tm.UpdateStatus(status);
}else{
byte[] imageBytes = WeiboUtils.readFileImage(fileName);
weibo5j.http.ImageItem item = new weibo5j.http.ImageItem(imageBytes);
tm.UploadStatus(status, item);
}
}
}
/**
* @Title: WeiboUtils.java
* @Package com.tuan.web.service.weibo.util
* @Description: TODO(添加描述)
* @author longzhun
* @date 2012-7-23 下午07:36:10
* @version V1.0
*/
public class WeiboUtils {
public static byte[] readFileImage(String filename)throws IOException{
URL url = new URL(filename);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
InputStream inStream = conn.getInputStream();
ByteArrayOutputStream outsStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while((len = inStream.read(buffer))!=-1){
outsStream.write(buffer,0,len);
}
outsStream.close();
inStream.close();
return outsStream.toByteArray();
}
}
@RequestMapping(value = "/bindSina2",method=RequestMethod.GET)
public String bindSina2(QueryForm queryForm, String goodsId,ModelMap modelMap, HttpServletRequest request,
HttpServletResponse response) {
String returnurl = "";
try{
returnurl = sinaWeiboV2.getRequestToken();
}catch(Exception e){
e.printStackTrace();
logger.error("bindSina is error "+e,e);
}
return "redirect:"+returnurl;
}
@RequestMapping(value = "/forwardSinaweibo2")
public String forwardSinaweibo2(QueryForm queryForm, String code,ModelMap modelMap, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try{
sinaWeiboV2.setToken(code);
String uid = "1730077315";
sinaWeiboV2.getUserId(); //获取用户Id 绑定窝窝账号
sinaWeiboV2.createFriend(uid); //添加关注
sinaWeiboV2.publicMsg(URLEncoder.encode("封装完毕:新浪围博客Oauth2.0","UTF-8"),
"http://ww1.sinaimg.cn/bmiddle/7e14fe22jw1dvbdwfyvpoj.jpg "); //发布围脖
}catch(Exception e){
logger.error("异常信息:"+e,e);
return "error";
}
return "error";
}