新浪公开平台使用

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.List;

import weibo4j.Status;

import weibo4j.Weibo;

import weibo4j.WeiboException;

public class WeiboInvoke {

/**

* @param args

*/

public static void main(String[] args) throws Exception {

String token="token"; //新流的token

String tokenSecret="tokenSecret";

// 获取所有微博

List<Status> list = getUserTimeline(token,tokenSecret);

for (Status status : list) {

System.out.println(status.getId());

}

//System.out.println("--------------------------");

// 发博

// sendWeibo("又是一条11111");

//System.out.println("--------------------------");

//deleteWeibo();

System.out.println("--------------------------");

File file=new File("E:\\assets\\mousePic\\1uparrow2.png");

sendWeibo(token,tokenSecret,"这是图片贴子",file);

}

/**

* byte 读入

* @param ins

* @throws IOException

*/

public static void byteRead(InputStream ins) throws IOException {

byte[] b = new byte[1];

String body = "";

while (ins.read(b) != -1) {

body += new String(b, "utf-8");

}

System.out.println(body);

}

/**

* BufferedReader 读入

* @throws IOException

*/

public static void BufferRead() throws IOException {

URL url = new URL(

"http://api.t.sina.com.cn/statuses/public_timeline.json?source=3982668722");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

InputStream ins = conn.getInputStream();

BufferedInputStream bs = new BufferedInputStream(ins);

BufferedReader ir = new BufferedReader(new InputStreamReader(bs));

System.out.println(ir.readLine());

}

/**

* 获取所有发的微博

* @return

* @throws Exception

*/

public static List<Status> getUserTimeline(String token, String tokenSecret) throws Exception {

Weibo weibo = new Weibo();

weibo.setOAuthConsumer(Weibo.CONSUMER_KEY, Weibo.CONSUMER_SECRET);

weibo.setOAuthAccessToken(token, tokenSecret);

List<Status> listStatus = weibo.getUserTimeline();

return listStatus;

}

/**

* 发送微博 (不含图片)

* @param text

*/

public static void sendWeibo(String token, String tokenSecret,String text) {

Weibo weibo = new Weibo();

weibo.setOAuthConsumer(Weibo.CONSUMER_KEY, Weibo.CONSUMER_SECRET);

weibo.setOAuthAccessToken(token, tokenSecret);

Status status = null;

try {

status = weibo.updateStatus(text);

} catch (WeiboException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(status.getId());

}

/**

* 发送微博 (含图片)

* @param text

* @param fileImage

*/

public static void sendWeibo(String token, String tokenSecret,String text,File file)  throws  IOException{

 

Weibo weibo = new Weibo();

weibo.setOAuthConsumer(Weibo.CONSUMER_KEY, Weibo.CONSUMER_SECRET);

weibo.setOAuthAccessToken(token, tokenSecret);

Status status = null;

try {

status =weibo.uploadStatus(java.net.URLEncoder.encode(text,"utf-8"), file);

} catch (WeiboException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(status.getId());

}

/**

* 删除发送的微博

*/

public static void deleteWeibo(String token, String tokenSecret) {

Weibo weibo = new Weibo();

weibo.setOAuthConsumer(Weibo.CONSUMER_KEY, Weibo.CONSUMER_SECRET);

weibo.setOAuthAccessToken(token, tokenSecret);

Status status = null;

try {

status = weibo.destroyStatus(3368899123770855l);

} catch (WeiboException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println(status.getUser().isVerified());

}

}


猜你喜欢

转载自javaeedevelop.iteye.com/blog/1246488