????????前面章節已經介紹使用code換取Token的整個流程了,這里不再重復闡述了,下面我們獲取到用戶token以后如何幫用戶自動關注別人。需要參數關注者的用戶ID(token授權用戶)以及關注的目標用戶ID。用戶ID如何獲取可以看上一章節獲取用戶信息里面就有用戶ID參數。
1.引入相關依賴Maven
<dependency><groupId>oauth.signpost</groupId><artifactId>signpost-core</artifactId><version>1.2.1.2</version>
</dependency><dependency><groupId>oauth.signpost</groupId><artifactId>signpost-commonshttp4</artifactId><version>1.2.1.2</version>
</dependency><dependency><groupId>com.twitter</groupId><artifactId>twitter-api-java-sdk</artifactId><version>1.1.4</version>
</dependency><dependency><groupId>commons-httpclient</groupId><artifactId>commons-httpclient</artifactId><version>3.1</version>
</dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>29.0-jre</version>
</dependency>
2.相關的配置類
/*** 推特相關配置*/
public class TwitterConfig {/*** 客戶id和客戶私鑰*/public static final String CLIENT_ID = "c3dqY111tjbnFPNDM6MTpjaQ";public static final String CLIENT_SECRET = "kf1119fmdeXZHpOV-fjv9umx55ZdccCkNONjea";/*** 應用KYE和私鑰*/public static final String CONSUMER_KEY = "lhyfiD111MffGeHMR";public static final String CONSUMER_SECRET = "BRNxnV5Lx111jtptduIkcwjB";/*** 應用的TOKEN*/public static final String ACCESS_TOKEN = "14821111633-A8xyN5111FgkbStu";public static final String ACCESS_TOKEN_SECRET = "oZaKBphpoo111SZvzoXPAQ";}
@Data
@Accessors(chain = true)
public class AttentionDto {/*** true 表示關注成功 false 表示關注失敗(目標用戶沒有公開推文的情況,因為他們必須批準關注者請求)*/private Boolean following;/*** 指示目標用戶是否需要批準關注請求。請注意,只有當目標用戶批準傳入的關注者請求時,經過身份驗證的用戶才會關注目標用戶 false就是正常的*/private Boolean pending_follow;}
/*** 用戶關注* @param token 授權換取的用戶token* @param userId 用戶自己的ID* @return* @throws Exception*/public AttentionDto attention(String token, String userId) throws Exception {String urlAdress = "https://api.twitter.com/2/users/" + userId + "/following";URL url12 = new URL(urlAdress);HttpURLConnection connection = (HttpURLConnection) url12.openConnection();connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type", "application/json");connection.setRequestProperty("Authorization", "Bearer " + token);JSONObject requestBody = new JSONObject();//需要關注的用戶IDrequestBody.put("target_user_id", "148294855969111111");String requestBodyString = requestBody.toString();connection.setDoOutput(true);OutputStream outputStream = connection.getOutputStream();outputStream.write(requestBodyString.getBytes());outputStream.flush();outputStream.close();int responseCode = connection.getResponseCode();InputStream inputStream;if (responseCode >= 200 && responseCode < 400) {inputStream = connection.getInputStream();} else {inputStream = connection.getErrorStream();}BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String line;StringBuilder responseBuilder = new StringBuilder();while ((line = reader.readLine()) != null) {responseBuilder.append(line);}reader.close();String response = responseBuilder.toString();if(JSON.parseObject(response).get("data") != null){JSONObject json = JSON.parseObject(JSON.parseObject(response).get("data").toString());AttentionDto attentionDto = new AttentionDto();Object ret = json.get("following");attentionDto.setFollowing(Boolean.parseBoolean(ret == null ? "false" : ret.toString()));ret = json.get("pending_follow");attentionDto.setPending_follow(Boolean.parseBoolean(ret == null ? "false" : ret.toString()));return attentionDto;}return null;}