自動釋出新浪微博java

banq發表於2011-07-09
準備給JiveJdon增加一個關注提交微博功能,一個使用者關注某個話題或作者,如果有更新,將更新內容提交其微博上,新浪微博使用OAuth認證,一般中間需要一個介面輸入使用者名稱和密碼,因為是關注後自動啟用提交,所以由程式自動提交,當然微博的使用者和密碼當使用者決定關注話題時需要輸入,並儲存在jivejdon系統中,這是有一些風險。

在新浪微博原來的weibo案例中增加一些程式碼,直接執行WebAuth這個Application即可,前提是:
1. 將Weibo中的CONSUMER_KEY CONSUMER_SECRET替換成你註冊新浪微博應用後給你的值。
2. 下面程式碼中替換你的新浪微博的使用者名稱和密碼。

package weibo4j.examples;

package weibo4j.examples;

import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import weibo4j.Status;
import weibo4j.Weibo;
import weibo4j.WeiboException;
import weibo4j.http.AccessToken;
import weibo4j.http.HttpClient;
import weibo4j.http.PostParameter;
import weibo4j.http.RequestToken;
import weibo4j.http.Response;

public class WebOAuth {

	public static String CONSUMER_KEY = "2299894210";//你的新浪應用key 預設是"jivejdon"的
	public static String CONSUMER_SECRET = "191b3411c5f126176b98e1be26b02233";
	public static String SINA_USERID = "你的新浪微博使用者名稱";
	public static String SINA_PASSWD = "你的新浪微博密碼";

	public static RequestToken request(String backUrl) {
		try {
			System.setProperty("weibo4j.oauth.consumerKey", CONSUMER_KEY);
			System.setProperty("weibo4j.oauth.consumerSecret", CONSUMER_SECRET);

			Weibo weibo = new Weibo();
			RequestToken requestToken = weibo.getOAuthRequestToken(backUrl);

			System.out.println("Got request token.");
			System.out.println("Request token: " + requestToken.getToken());
			System.out.println("Request token secret: " + requestToken.getTokenSecret());
			return requestToken;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static AccessToken requstAccessToken(RequestToken requestToken, String verifier) {
		try {
			System.setProperty("weibo4j.oauth.consumerKey", CONSUMER_KEY);
			System.setProperty("weibo4j.oauth.consumerSecret", CONSUMER_SECRET);

			Weibo weibo = new Weibo();
			AccessToken accessToken = weibo.getOAuthAccessToken(requestToken.getToken(), requestToken.getTokenSecret(), verifier);

			System.out.println("Got access token.");
			System.out.println("access token: " + accessToken.getToken());
			System.out.println("access token secret: " + accessToken.getTokenSecret());
			return accessToken;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void update(AccessToken access, String content) {
		try {
			Weibo weibo = new Weibo();
			weibo.setToken(access.getToken(), access.getTokenSecret());
			Status status = weibo.updateStatus(content);
			System.out.println("Successfully updated the status to [" + status.getText() + "].");
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		RequestToken requestToken = request("xml");
		HttpClient http = new HttpClient();
		PostParameter[] postParameters = new PostParameter[] { new PostParameter("oauth_callback", "xml"), new PostParameter("userId", SINA_USERID),
				new PostParameter("passwd", SINA_PASSWD) };
		Response response = http.get(requestToken.getAuthorizationURL());
		response = http.post(requestToken.getAuthorizationURL(), postParameters);
		Weibo weibo = new Weibo();

		System.out.print("www" + response.asString());
		StringReader reader = new StringReader(response.asString());
		InputSource inputSource = new InputSource(reader);
		Map map = readXML(inputSource);

		AccessToken accessToken = requstAccessToken(requestToken, (String) map.get("oauth_verifier"));
		update(accessToken, "測試一下");

	}

	public static Map readXML(InputSource inputSource) {
		Map map = new HashMap();
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

			Document doc = docBuilder.parse(inputSource);

			// normalize text representation
			doc.getDocumentElement().normalize();
			System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());

			map.put("oauth_token", getValue(doc, "oauth_token"));
			map.put("oauth_verifier", getValue(doc, "oauth_verifier"));
			System.out.println(" oauth_token=" + getValue(doc, "oauth_token"));
			System.out.println(" oauth_token=" + getValue(doc, "oauth_verifier"));

		} catch (Exception err) {

			System.out.println(" " + err.getMessage());

		}
		return map;

	}

	public static String getValue(Document doc, String name) {
		String ret = "";
		NodeList listOfPersons = doc.getElementsByTagName(name);
		for (int s = 0; s < listOfPersons.getLength(); s++) {
			Node firstPersonNode = listOfPersons.item(s);
			Element firstPersonElement = (Element) firstPersonNode;
			ret = firstPersonElement.getTextContent();

		}// end of for loop with s var
		return ret;

	}
}


<p class="indent">

[該貼被banq於2011-07-09 17:13修改過]

[該貼被admin於2011-07-09 17:17修改過]

[該貼被admin於2011-07-24 14:46修改過]

相關文章