微博SDK介紹
項目需求,需要開發一個新浪微博回覆機器人的小工具。通過調研,使用新浪微博開放的公共API接口是最方便的。
使用新浪微博開放平臺的API接口,可以輕鬆實現微博的查詢,用戶的查詢,微博的回覆等各項功能。使用官方提供的SDK工具,可以實現代碼自動進行微博的模擬操作行爲。官方提供了多種語言的SDK工具,包括Python包(由廖雪峯老師提供),本文使用的Java包等。
SDK使用方法
申請應用
-
申請應用:由於需要進行頁面接口的開發,到微博開放平臺-微連接申請網站接入。如下圖:
-
開發者資料完善:在創建應用後,需要完善個人身份,填寫開發者資料,如下圖所示。這裏注意,後續的身份認證步驟可以根據自身需要完成,不一定必須認證。
-
填寫回調地址:在應用申請之後,需要到完成的應用裏將信息進一步完善纔可以使用java SDK工具。具體的,點擊“我的應用”-選中創建好的應用-點擊“應用信息”-“高級信息”-填寫“OAuth2.0 授權設置”,這裏可以填寫如下。授權回調頁會在之後的授權過程中用到,不填寫這一項程序無法執行。取消授權回調頁功能並不支持,因此可以隨意填寫。
-
應用申請結束,將幾個關鍵信息記住,用於代碼中參數的設置。包括App Key, App Secret, 回調地址等。如下圖:
應用開發
接口的開發可以參考官方wiki,或者在自己的“接口管理”模塊中查看目前可以使用的API接口信息。
代碼導入
原作者使用eclipse進行開發,使用intelliJ IDEA開發的同學可以直接進行導入,選擇eclipse導入即可。
代碼中使用的jar包直接放到了lib目錄下,我們可以使用maven進行相應jar包的導入,主要包含日誌包和一個http包。具體依賴如下:
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0-alpha1</version>
</dependency>
</dependencies>
將用於參數設置的properties文件放到resources目錄下,並重新設置參數值,將申請到的應用具體參數填入。具體如下:
代碼結構
如上圖所示,
- http包下代碼主要用於發送和接收請求
- model包將weibo中用到的實體對象進行了封裝。包括Comment、MySSLSocketFactory、Trend等對象
- oauth包是我自己添加的,其中的代碼用於設置accessToken,具體的,在將config.properties文件中參數進行修改後,運行包下面的OAuth4Code.java,會自動跳轉到授權頁面,在重定向的URL最後會攜帶code參數,將code參數填入,將會產生最終的accessToken,這個accessToken值將會是我們之後進行查詢、回覆等接口開發的身份認證信息。
- org.json包下用於查詢、回覆等步驟的JSON數據的解析
- utils包下的代碼包含數據的封裝類、數據編碼轉換類等實用類。
回覆代碼
代碼構建
完成上述步驟之後,我們進行工程的編譯和構建。使用maven進行install,將編譯完成的jar包放到本地的maven倉庫中,用於後續接口的調用。
整體思路
在pom文件中,將我們之前編譯好的maven項目導入。要想回復別人的微博,主要需要獲取目標微博的ID。微博ID無法進行直接查詢,我們必須通過目標用戶進行間接的查詢。想要獲取目標用戶,我們需要首先將開發用的微博賬號對目標賬號進行關注,這樣我們就可以獲取目標用戶的時間線。通過將時間線中的微博進行存儲,我們就可以完成對目標微博的評論工作。
回覆代碼
具體代碼如下:
package xx.xxx.xxx.service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import weibo4j.Comments;
import weibo4j.Timeline;
import weibo4j.Users;
import weibo4j.model.Comment;
import weibo4j.model.CommentWapper;
import weibo4j.model.Status;
import weibo4j.model.StatusWapper;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2020/4/28.
* 應用開發者需要對待評論用戶進行關注
* 獲取開發者時間線上的微博,這樣就可以獲取所有帶評論的微博ID
* 將所有歷史微博的正文信息以及mid保存到數據庫中
* 通過回覆程序進行回覆
*/
@Service
public class WeiboReplyService {
private static final Logger LOGGER = LoggerFactory.getLogger(WeiboReplyService.class);
@Value("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
private String accessToken;
public WeiboReplyService(String accessToken) {
this.accessToken = accessToken;
}
public WeiboReplyService() {
}
/**
* 根據用戶暱稱獲取用戶uid
* @param accessToken
* @param userNickName 用戶暱稱
* @return 用戶信息
* @throws WeiboException
*/
public User userSearch(String accessToken, String userNickName) throws WeiboException {
Users users = new Users(accessToken);
User user = users.showUserByScreenName(userNickName);
LOGGER.info(user.toString());
return user;
}
public List<String> getFriendsTimeline() throws WeiboException {
// String uID = user.getId();
Timeline timeline = new Timeline(this.accessToken);
StatusWapper friendsTimeline = timeline.getFriendsTimeline();
LOGGER.info(friendsTimeline.toString());
List<Status> statuses = friendsTimeline.getStatuses();
ArrayList<String> StatusIDs = new ArrayList<>();
if (!statuses.isEmpty()) {
for (Status status : statuses) {
LOGGER.info(status.toString());
StatusIDs.add(status.getId());
}
}
return StatusIDs;
}
/**
* 對目標微博進行評論
* @param commentContent 評論內容
* @param targetMessageID 目標微博ID
*/
public void commentCreate(String commentContent, String targetMessageID) {
Comments cm = new Comments(accessToken);
try {
Comment comment = cm.createComment(commentContent, targetMessageID);
LOGGER.info(comment.toString());
} catch (WeiboException e) {
e.printStackTrace();
}
}
/**
* 獲取當前登錄用戶的最新評論包括接收到的與發出的
* @param accessToken
*/
public void getCommentTimeline(String accessToken) {
Comments cm = new Comments(accessToken);
try {
CommentWapper commentTimeline = cm.getCommentTimeline();
List<Comment> comments = commentTimeline.getComments();
for (Comment comment : comments) {
LOGGER.info(comment.toString());
}
} catch (WeiboException e) {
e.printStackTrace();
}
}
}
測試程序如下:
package xx.xxx.xxx.dao;
import xx.xxx.xxx.service.WeiboReplyService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import weibo4j.model.WeiboException;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* Created by Administrator on 2020/4/13.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class ServiceTest {
private static Logger LOGGER = LoggerFactory.getLogger(ServiceTest.class);
@Autowired
private WeiboReplyService weiboReplyService;
@Test
public void autoReplyTest() throws UnsupportedEncodingException {
String commentContent = "Hello World!這是一條來自IntelliJ IDEA客戶端的JAVA回覆...";
// 這裏的targetMessageID用你想要回復的微博ID進行替換
String targetMessageID = "XXXXXXXXXXXXXXXX";
weiboReplyService.commentCreate(commentContent, targetMessageID);
}
@Test
public void weiboTest() throws WeiboException {
String content = "我是一個無情的回覆機器...";
List<String> friendsTimelineStatusIDs = weiboReplyService.getFriendsTimeline();
for (String statusID : friendsTimelineStatusIDs) {
weiboReplyService.commentCreate(content, statusID);
}
LOGGER.info("回覆結束...");
}
}
運行測試程序,有以下日誌輸出:
[DEBUG] 2020-04-30 01:32:49,585 method:org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$ConnectionPool.notifyWaitingThread(MultiThreadedHttpConnectionManager.java:961)
Notifying no-one, there are no waiting threads
[DEBUG] 2020-04-30 01:32:49,588 method:weibo4j.http.HttpClient.log(HttpClient.java:155)
...評論對象的JSON串...
最後,到頁面上查看一下
大功告成!!!