private void initSinaWeibo() { weibo = new WeiboExt(); weibo.setHttpConnectionTimeout(5000); if (sinaToken == null) sinaToken = SinaAccountUtil.getToken(PageActivity.this); weibo.setToken(sinaToken.getToken(), sinaToken.getTokenSecret()); }
private static Weibo getWeibo(boolean isOauth, String[] args) { Weibo weibo = new Weibo(); if (isOauth) { // oauth验证方式 args[0]:访问的token;args[1]:访问的密匙 weibo.setToken(args[0], args[1]); } else { // 用户登录方式 weibo.setUserId(args[0]); // 用户名/ID weibo.setPassword(args[1]); // 密码 } return weibo; }
/** * 添加收藏 * * @param args args[0]和args[1]为通过GetToken.java获取到的accesstoken和accesstoken secret * args[2]为将要收藏的statusId */ public static void main(String[] args) { System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET); try { Weibo weibo = new Weibo(); weibo.setToken(args[0], args[1]); Status status = weibo.createFavorite(Long.parseLong(args[2])); System.out.println(status.toString()); } catch (WeiboException e) { e.printStackTrace(); } }
public static void main(String[] args) { String access_token = args[0]; String mid = args[1]; Weibo weibo = new Weibo(); weibo.setToken(access_token); Timeline tm = new Timeline(); try { JSONObject id = tm.QueryId(mid, 1, 1); Log.logInfo(String.valueOf(id)); } catch (WeiboException e) { e.printStackTrace(); } }
public static void main(String[] args) { String access_token = args[0]; Weibo weibo = new Weibo(); weibo.setToken(access_token); String uids = args[1]; Users um = new Users(); try { JSONArray user = um.getUserCount(uids); Log.logInfo(user.toString()); } catch (WeiboException e) { e.printStackTrace(); } }
public static void main(String[] args) { String access_token = args[0]; Weibo weibo = new Weibo(); weibo.setToken(access_token); String trend_name = args[1]; Trend tm = new Trend(); try { JSONObject result = tm.isFollow(trend_name); Log.logInfo(String.valueOf(result)); } catch (WeiboException e) { e.printStackTrace(); } }
/** * 获取当前用户发出的评论 * * @param args */ public static void main(String[] args) { System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET); try { Weibo weibo = getWeibo(true, args); List<Comment> comments = weibo.getCommentsTimeline(); for (Comment comment : comments) { System.out.println(comment.toString()); } } catch (Exception e) { e.printStackTrace(); } }
/** @param args */ public static void main(String[] args) { String access_token = args[0]; Weibo weibo = new Weibo(); weibo.setToken(access_token); Timeline tm = new Timeline(); try { List<Status> status = tm.getRepostByMe(); for (Status s : status) { Log.logInfo(s.toString()); } } catch (WeiboException e) { e.printStackTrace(); } }
/** * 注册新浪微博用户接口 * * @param args */ public static void main(String[] args) { System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET); try { Weibo weibo = getWeibo(false, args); // IP必须被平台列入白名单方可执行注册接口 String IP = InetAddress.getLocalHost().getHostAddress(); // args参数从第三个开始为昵称、性别、密码、邮箱; JSONObject object = weibo.register(IP, "", "", "nick", "m", "passwd", "*****@*****.**"); System.out.println("Your ID:" + object.getString("uid")); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { String access_token = args[0]; Weibo weibo = new Weibo(); weibo.setToken(access_token); Favorite fm = new Favorite(); String tid = args[1]; try { List<Favorites> favors = fm.getFavoritesByTags(tid); for (Favorites s : favors) { Log.logInfo(s.toString()); } } catch (WeiboException e) { e.printStackTrace(); } }
// 设置一个access_token供爬虫使用 public static void setOneAccessToken() { // 随机提供 Random rand = new Random(); int len = accessToken.size(); int index = Math.abs(rand.nextInt() % len); weibo.setToken(accessToken.get(index)); }
/** @param args */ public static void main(String[] args) { String access_token = args[0]; Weibo weibo = new Weibo(); weibo.setToken(access_token); String uid = args[1]; Trend tm = new Trend(); List<UserTrend> trends = null; try { trends = tm.getTrends(uid); for (UserTrend t : trends) { Log.logInfo(t.toString()); } } catch (WeiboException e) { e.printStackTrace(); } }
/** * 返回用户关注对象列表,并返回最新微博文章。 * * @param args */ public static void main(String[] args) { System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET); try { Weibo weibo = new Weibo(); weibo.setToken(args[0], args[1]); int cursor = 5; // 采用cursor参数处理翻页 List<User> list = weibo.getFollowersStatuses("1377583044", cursor); for (User user : list) { System.out.println(user.toString()); } } catch (WeiboException e) { e.printStackTrace(); } }
/** @param args */ public static void main(String[] args) { String access_token = args[0]; Weibo weibo = new Weibo(); weibo.setToken(access_token); Tags tm = new Tags(); String ids = args[1]; List<Tag> tags = null; try { tags = tm.destroyTagsBatch(ids); for (Tag t : tags) { Log.logInfo(t.toString()); } } catch (WeiboException e) { e.printStackTrace(); } }
/** * 获取tags; * * @param args args[0]和args[1]为通过GetToken.java获取到的accesstoken和accesstoken secret args[2]为获取用户的Id */ public static void main(String[] args) { System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET); try { Weibo weibo = new Weibo(); weibo.setToken(args[0], args[1]); Paging paging = new Paging(); paging.setCount(20); paging.setPage(1); List<Tag> gettags = weibo.getTags(args[2], paging); for (Tag status : gettags) { System.out.println(status.toString()); } } catch (Exception e) { e.printStackTrace(); } }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY); System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET); Paging paging = new Paging(); paging.setCount(20); paging.setPage(1); try { Weibo weibo = new Weibo(); weibo.setToken(args[0], args[1]); List<Trends> trendsWeekly = weibo.getTrendsWeekly(0); System.out.println("=======按周返回热门话题======="); System.out.println(trendsWeekly); } catch (WeiboException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public void forward(String comment, String url) throws WeiboException, NoSinaAccountBindedException { String userId = preferences.getString("sina_account", null); if (userId == null) throw new NoSinaAccountBindedException(); if (weibo == null) { initSinaWeibo(); } weibo.updateStatus(comment + " " + url); }
@Override protected String getNickName(Map<String, Object> oParaMap) { AccessToken accessToken = (AccessToken) oParaMap.get("token"); weibo.setToken(accessToken.getAccessToken()); try { User ur = um.showUserById(accessToken.getUid()); return ur.getScreenName(); } catch (WeiboException e) { log.error("获取新浪昵称失败", e); } return null; }
// 验证用户 // 错误的用户/密码 // 警告: 使用错误用户/密码登录会导致帐号被锁定,该测试不能在时间段内运行超过3次 @Test public void testVerifyCredentials() { try { Weibo fanfou = new Weibo("*****@*****.**", "19851129a"); User user = fanfou.verifyCredentials(); } catch (WeiboException e) { switch (e.getStatusCode()) { case 401: System.out.println("not a user!"); break; case 200: fail(); System.out.println("is a user!"); break; default: fail(); } } }
/** * @param weibo * @param id * @return the latest retweet for tweet id with maximum number of Countmax * @throws WeiboException */ public ArrayList<Status> getRepostTimeline(Weibo weibo, String id) throws WeiboException { int Countmax = 200; Paging paging = new Paging(); paging.setCount(Countmax); paging.setSinceId(3363472209835882L); ArrayList<Status> statuses = (ArrayList<Status>) weibo.getreposttimeline(id, paging); /* * for(int i = 2; i<4; i++){ if(statuses.size()==200*(i-1)){ * paging.setPage(i); statuses.addAll(weibo.getreposttimeline(id, * paging)); }else{ break; } } */ return statuses; }
private ArrayList<Status> getUserReposts(Weibo weibo, String id) throws WeiboException { // TODO Auto-generated method stub int Countmax = 200; Paging paging = new Paging(); paging.setCount(Countmax); paging.setSinceId(3363472209835882L); ArrayList<Status> statuses = (ArrayList<Status>) weibo.getrepostbyme(id, paging); /* * for(int i = 2; i<4; i++){ if(statuses.size()==200*(i-1)){ * paging.setPage(i); statuses.addAll(weibo.getreposttimeline(id, * paging)); }else{ break; } } */ return statuses; }