sina微博开放平台使用
程序员文章站
2022-03-05 14:47:30
...
微博太火了,跟我来先体验一把sina微博开发。
1、登录sina微博,点击“应用”
2、点击“微博开发平台 我也要做开发者”
3、点击“我的应用”,填写“开发者信息”
4、点击“创建应用”,就是你将要开发的微博应用程序,可以是电脑客户端,微博推广,手机客户端等。
5、获取App Key, App Secret
6、下载SDK,针对不同的开发,有不同语言版本,我下载的是:weibo4j-2010-12-27.zip
7、在Eclipse中打开。注意SDK要在jdk1.6下编译,不然会有错误。
8、源代码中:weibo4j.Weibo.java
public static String CONSUMER_KEY = "";
public static String CONSUMER_SECRET = "";
替换成你的App Key和App Secret
9、OK,全部都是源代码给你了,还有example
10、试试看发一条微博:
右击Update.java 选择Run As->open run Dialogue... 选择update,然后选Arguments选项卡 输入你的用户名 (每个参数中间有空格)、密码、和发布的微博 运行一下,OK!
注意:
如果您使用的是代理服务器,那么必须重写 weibo4j.http.HttpClient类的httpRequest方法
代码如下
public Response httpRequest(String url, PostParameter[] postParams,
boolean authenticated, String httpMethod) throws WeiboException {
int retriedCount;
int retry = retryCount + 1;
Response res = null;
for (retriedCount = 0; retriedCount < retry; retriedCount++) {
int responseCode = -1;
try {
String proxy="172.17.18.80";//代理服务器名
String port="8080";
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);
HttpURLConnection con = null;
OutputStream osw = null;
try {
con = getConnection(url);
con.setDoInput(true);
setHeaders(url, postParams, con, authenticated, httpMethod);
if (null != postParams || "POST".equals(httpMethod)) {
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
con.setDoOutput(true);
String postParam = "";
if (postParams != null) {
postParam = encodeParameters(postParams);
}
log("Post Params: ", postParam);
byte[] bytes = postParam.getBytes("UTF-8");
con.setRequestProperty("Content-Length",
Integer.toString(bytes.length));
osw = con.getOutputStream();
osw.write(bytes);
osw.flush();
osw.close();
} else if ("DELETE".equals(httpMethod)){
con.setRequestMethod("DELETE");
} else {
con.setRequestMethod("GET");
}
res = new Response(con);
responseCode = con.getResponseCode();
if(DEBUG){
log("Response: ");
Map<String, List<String>> responseHeaders = con.getHeaderFields();
for (String key : responseHeaders.keySet()) {
List<String> values = responseHeaders.get(key);
for (String value : values) {
if(null != key){
log(key + ": " + value);
}else{
log(value);
}
}
}
}
if (responseCode != OK) {
if (responseCode < INTERNAL_SERVER_ERROR || retriedCount == retryCount) {
throw new WeiboException(getCause(responseCode) + "\n" + res.asString(), responseCode);
}
// will retry if the status code is INTERNAL_SERVER_ERROR
} else {
break;
}
} finally {
try {
osw.close();
} catch (Exception ignore) {
}
}
} catch (IOException ioe) {
// connection timeout or read timeout
if (retriedCount == retryCount) {
throw new WeiboException(ioe.getMessage(), ioe, responseCode);
}
}
try {
if(DEBUG && null != res){
res.asString();
}
log("Sleeping " + retryIntervalMillis +" millisecs for next retry.");
Thread.sleep(retryIntervalMillis);
} catch (InterruptedException ignore) {
//nothing to do
}
}
return res;
}
上一篇: C++语言标识符的命名规则是什么?
下一篇: c语言字符串结束标志是什么