weiboclient4j微博客戶端API
為什么需要另外一個(gè)Java版本的微博客戶端?
新浪微博官方推薦的Java客戶端 weibo4j 一直沒(méi)有發(fā)布到maven倉(cāng)庫(kù),而我們是重度maven用戶,因而重新發(fā)明了這個(gè)新的輪子。
通過(guò)maven引用weiboclient4j
在項(xiàng)目pom.xml里面加入依賴(lài):
com.github.hoverruan weiboclient4j 0.4.13
使用
Weiboclient4j支持新浪微博API V1和V2(未完成),目前推薦使用V2版本的接口:
// 使用你的應(yīng)用的api key和secret String apiKey = "xxxxxxx"; String apiSecret = "xxxxxxxx"; WeiboClient client = new WeiboClient(apiKey, apiSecret);
OAuth2例子:
String authorizationCallback = "..."; // 你的Callback地址
String state = "...";
String url = client.getAuthorizationUrl(ResponseType.Code, DisplayType.Default, state, authorizationCallback); // 瀏覽器重定向到url; 用戶授權(quán); 然后返回callback地址
String code = ... // 從新浪的回調(diào)請(qǐng)求里面獲得code String accessTokenCallback = "..."; // 或者Access Token的Callback地址
SinaWeibo2AccessToken accessToken = client.getAccessTokenByCode(code, accessTokenCallback);
System.out.println("Access token: " + accessToken.getToken());
System.out.println("User Uid: " + accessToken.getUid());
System.out.println("Expires in: " + accessToken.getExpiresIn());
System.out.println("Remind in: " + accessToken.getRemindIn());
獲取用戶Timeline例子:
StatusService service = client.getStatusService(); Timeline friendsTimeline = service.getFriendsTimeline();
更多的使用例子可以參考 weiboclient4j.examples.OAuth2CommandLine
API參數(shù)對(duì)象化
WeiboClient里面,大部分的方法都沒(méi)有Javadoc,取而代之的是大部分的參數(shù)都是特定的對(duì)象,這樣做的原因是因?yàn)椋?/p>
-
寫(xiě)Javadoc太麻煩
-
一些API的參數(shù)較多,如果使用基本類(lèi)型容易混淆各個(gè)參數(shù)的含義
-
IDE對(duì)已知類(lèi)型的對(duì)象、Enum能提供更友好的提醒和自動(dòng)完成
所有的參數(shù)對(duì)象在 package weiboclient4j.params 下面;舉一個(gè)例子:
使用靜態(tài)引入 CoreParameters.\*:
import static weiboclient4j.params.CoreParameters.*;
FriendshipService service = client.getFriendshipService();
Friendship friendship;
friendship = service.getFriendship(sourceUid(12345), targetUid(67890));
// 或者
friendship = service.getFriendship(sourceScreenName("xxx"), targetScreenName("yyy"));
使用 P:
import weiboclient4j.params.P;
FriendshipService service = client.getFriendshipService();
Friendship friendship;
friendship = service.getFriendship(P.sourceUid(12345), P.targetUid(67890));
// 或者
friendship = service.getFriendship(P.sourceScreenName("xxx"), P.targetScreenName("yyy"));
分頁(yè)對(duì)象 Paging
分頁(yè)相關(guān)的參數(shù),全部通過(guò) Paging 對(duì)象封裝:
Paging paging = Paging.create() .sinceId(12345) .count(25); Timeline timeline = service.getFriendsTimeline(paging, BaseApp.No, Feature.All); // 后兩個(gè)參數(shù)可省略
新浪微博API V2支持情況
| 微博接口 | StatusService | 完成 |
| 評(píng)論接口 | CommentService | 完成 |
| 用戶接口 | UserService | 完成 |
| 關(guān)系接口 | FriendshipService | 完成 |
| 帳號(hào)接口 | AccountService | 完成 |
| 收藏接口 | FavoriteService | 完成 |
| 話題接口 | TrendService | 完成 |
| 標(biāo)簽接口 | TagService | 完成 |
| 注冊(cè)接口 | RegisterService | 完成 |
| 搜索接口 | SearchService | 完成 |
| 推薦接口 | SuggestionService | 完成 |
| 提醒接口 | RemindService | 完成 |
| 短鏈接口 | ShortUrlService | 完成 |
| 通知接口 | NotificationService | 完成 |
| 公共服務(wù)接口 | CommonService | 完成 |
| 位置服務(wù)接口 | PlaceService | 完成 |
| 地理信息接口 | LocationService | 開(kāi)發(fā)中 |
評(píng)論
圖片
表情
