前面章节已经介绍使用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.相关的配置类
java
/**
* 推特相关配置
*/
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";
}
java
@Data
@Accessors(chain = true)
public class AttentionDto {
/**
* true 表示关注成功 false 表示关注失败(目标用户没有公开推文的情况,因为他们必须批准关注者请求)
*/
private Boolean following;
/**
* 指示目标用户是否需要批准关注请求。请注意,只有当目标用户批准传入的关注者请求时,经过身份验证的用户才会关注目标用户 false就是正常的
*/
private Boolean pending_follow;
}
java
/**
* 用户关注
* @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();
//需要关注的用户ID
requestBody.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;
}