java自动登入新浪微博-准备给JiveJdon增加一个关注提交微博功能
发布时间:2023-06-15 09:10 浏览次数:次 作者:佚名
准备给JiveJdon增加一个关注提交微博功能,一个用户关注某个话题或作者,如果有更新java自动登入新浪微博,将更新内容提交其微博上java自动登入新浪微博,新浪微博使用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; } }class=
"indent">
[该贴被banq于2011-07-09 17:13修改过]
[该贴被admin于2011-07-09 17:17修改过]
[该贴被admin于2011-07-24 14:46修改过]