在做一个需要调用微博接口的项目中.发现了一些问题
1.腾讯微博sdk的封装粒度不够,需要在其上做二次开发
2.新浪,腾讯微博没有实现统一的接口,在做项目的时候需要将它们分开来实现,代码看起来很别扭
我所做的就是解决了以上两个问题,所以现在做微博应用开发非常方便
统一接口
Java代码
/**
* @Description:微博接口
*/
public interface Weibo {
/**
* 得到RequestToken
* @param callbackUrl
* @return
*/
public RequestToken getRequestToken(String callbackUrl) throws Exception;
/**
* 得到AccessToken
* @param requestToken
* @param verifier
* @return
* @throws Exception
*/
public AccessToken getAccessToken(RequestToken requestToken,String verifier) throws Exception;
/**
*
* @param accessToken
*/
public void setAccessToken(AccessToken accessToken);
/**
* 得到用户信息
* @return
*/
public User getUserInfo() throws Exception;
/**
* friendNum是否已经被关注
* @param friendNum
* @return
*/
public boolean isFriends(String friendNum) throws Exception;
/**
* 关注friendNum
* @param friendNum
*/
public void createFriendship(String friendNum) throws Exception;
/**
* 发微博
* @param info 微博信息
* @param file 上传的图片文件,不上传文件传null
*/
public void publishMsg(String info, File file) throws Exception;
}
/**
* @Description:微博接口
*/
public interface Weibo {
/**
* 得到RequestToken
* @param callbackUrl
* @return
*/
public RequestToken getRequestToken(String callbackUrl) throws Exception;
/**
* 得到AccessToken
* @param requestToken
* @param verifier
* @return
* @throws Exception
*/
public AccessToken getAccessToken(RequestToken requestToken,String verifier) throws Exception;
/**
*
* @param accessToken
*/
public void setAccessToken(AccessToken accessToken);
/**
* 得到用户信息
* @return
*/
public User getUserInfo() throws Exception;
/**
* friendNum是否已经被关注
* @param friendNum
* @return
*/
public boolean isFriends(String friendNum) throws Exception;
/**
* 关注friendNum
* @param friendNum
*/
public void createFriendship(String friendNum) throws Exception;
/**
* 发微博
* @param info 微博信息
* @param file 上传的图片文件,不上传文件传null
*/
public void publishMsg(String info, File file) throws Exception;
}
开发步骤如下:
1.设置项目的key和secret(key,secret是用微博帐号申请的),只需要初始化一次
新浪:
Java代码
System.setProperty("weibo4j.oauth.consumerKey", "xxx");
System.setProperty("weibo4j.oauth.consumerSecret", "xxxxxxxxxx");
System.setProperty("weibo4j.oauth.consumerKey", "xxx");
System.setProperty("weibo4j.oauth.consumerSecret", "xxxxxxxxxx");
腾讯:
Java代码
System.setProperty("qqweibo.oauth.consumerKey","xxx");
System.setProperty("qqweibo.oauth.customSecret","xxxxxxxxxx");
System.setProperty("qqweibo.oauth.consumerKey","xxx");
System.setProperty("qqweibo.oauth.customSecret","xxxxxxxxxx");
2.传入微博帐号登录后需要返回的地址callbackUrl,得到requestToken和授权url
新浪:
Java代码
Weibo weibo = new SinaWeibo();
try {
RequestToken requestToken = weibo.getRequestToken(callbackUrl);
System.out.println(requestToken.getReqToken()+"-->"+requestToken.getReqTokenSecret());
System.out.println(requestToken.getAuthorizationURL());
} catch (Exception e) {
e.printStackTrace();
}
Weibo weibo = new SinaWeibo();
try {
RequestToken requestToken = weibo.getRequestToken(callbackUrl);
System.out.println(requestToken.getReqToken()+"-->"+requestToken.getReqTokenSecret());
System.out.println(requestToken.getAuthorizationURL());
} catch (Exception e) {
e.printStackTrace();
}
腾讯:
Java代码
Weibo weibo = new QQWeibo();
try {
RequestToken reqToken = weibo.getRequestToken(callbackUrl);
System.out.println(reqToken.getReqToken()+"-->"+ reqToken.getReqTokenSecret());
System.out.println(reqToken.getAuthorizationURL());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Weibo weibo = new QQWeibo();
try {
RequestToken reqToken = weibo.getRequestToken(callbackUrl);
System.out.println(reqToken.getReqToken()+"-->"+ reqToken.getReqTokenSecret());
System.out.println(reqToken.getAuthorizationURL());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
3. 在浏览器上输入授权url,用微博帐号登录后跳转到callbackUrl,获取callbackUrl后面的参数oauth_verifier的值,
通过传入requestToken,oauth_verifier的值得到AccessToken
新浪:
Java代码
Weibo weibo = new SinaWeibo();
try {
AccessToken accessToken = weibo.getAccessToken(requestToken, oauth_verifier);
System.out.println(accessToken.getAccessToken()+"-->"+ accessToken.getAccessTokenSecret());
} catch (Exception e) {
e.printStackTrace();
}
Weibo weibo = new SinaWeibo();
try {
AccessToken accessToken = weibo.getAccessToken(requestToken, oauth_verifier);
System.out.println(accessToken.getAccessToken()+"-->"+ accessToken.getAccessTokenSecret());
} catch (Exception e) {
e.printStackTrace();
} 腾讯:
Java代码
Weibo weibo = new QQWeibo();
try {
AccessToken accessToken = weibo.getAccessToken(reqToken, oauth_verifier);
System.err.println(accessToken.getAccessToken()+"-->"+ accessToken.getAccessTokenSecret());
} catch (Exception e) {
e.printStackTrace();
}
Weibo weibo = new QQWeibo();
try {
AccessToken accessToken = weibo.getAccessToken(reqToken, oauth_verifier);
System.err.println(accessToken.getAccessToken()+"-->"+ accessToken.getAccessTokenSecret());
} catch (Exception e) {
e.printStackTrace();
} 得到accessToken对象后就可以保存下来,以后调用关注好友接口,发微博接口都需要用到
4.调用获取用户接口
新浪:
Java代码
Weibo weibo = new SinaWeibo();
try {
weibo.setAccessToken(accessToken);
User user = weibo.getUserInfo();
System.out.println(user.getHuaming());
} catch (Exception e) {
e.printStackTrace();
}
Weibo weibo = new SinaWeibo();
try {
weibo.setAccessToken(accessToken);
User user = weibo.getUserInfo();
System.out.println(user.getHuaming());
} catch (Exception e) {
e.printStackTrace();
}
腾讯:
Java代码
Weibo weibo = new QQWeibo();
try {
weibo.setAccessToken(accessToken);
User user = weibo.getUserInfo();
System.out.println(user.getHuaming());
} catch (Exception e) {
e.printStackTrace();
}
Weibo weibo = new QQWeibo();
try {
weibo.setAccessToken(accessToken);
User user = weibo.getUserInfo();
System.out.println(user.getHuaming());
} catch (Exception e) {
e.printStackTrace();
} 5.调用关注接口,发微博等接口同4
最后我把封装的jar包发给大家