1. 目前新浪开放平台使用的是Oauthv2接口,认证协议还是Oauth(开放平台协议)
注:关于OAuth2.0协议本身的介绍,参见:http://www.cnblogs.com/highend/archive/2012/07/05/oauth2_introduced.html
注:里面最关键的就是access_token码,由appKey与appSecret经过加密而成的认证码.
2. 如何获取Appkey与AppSecretKey
登入:http://open.weibo.com/进行创建新的应用,输入个人信息与App的分类与介绍等信息,会发送邮件认证以及AppKey与AppSecretKey(这两个元素决定了App的唯一性)
3. 下载SDK
在http://open.weibo.com/上进行下载
4. 生成Access_token ID
这个是最关键的code信息,只要有这个就可以随意玩转新浪SDK
package weibo4j.examples.oauth2;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import weibo4j.Oauth;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;
public class OAuth4Code {
public static void main(String [] args) throws WeiboException, IOException{
Oauth oauth = new Oauth();
String[] arg = new String[]{"537567964","cab7fff7f00de8561f91059a4dfad638"};
BareBonesBrowserLaunch.openURL(oauth.authorize("code",arg[0],arg[1]));
System.out.println(oauth.authorize("code",arg[0],arg[1]));
System.out.print("Hit enter when it's done.[Enter]:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
Log.logInfo("code: " + code);
try{
System.out.println(oauth.getAccessTokenByCode(code));
} catch (WeiboException e) {
if(401 == e.getStatusCode()){
Log.logInfo("Unable to get the access token.");
}else{
e.printStackTrace();
}
}
}
5. 利用SDK发布新浪微博
package weibo4j.examples.timeline;
import weibo4j.Timeline;
import weibo4j.examples.oauth2.Log;
import weibo4j.model.Status;
import weibo4j.model.WeiboException;
public class UpdateStatus {
public static void main(String[] args) {
//String access_token = args[0];
String access_token = "2.00MhtmBC0CzZ4a375488fb670USpaF";
String statuses = "嘿嘿,我用新浪SDK发布微博了~~";
Timeline tm = new Timeline();
tm.client.setToken(access_token);
try {
Status status = tm.UpdateStatus(statuses);
Log.logInfo(status.toString());
} catch (WeiboException e) {
e.printStackTrace();
} }
}
小结:简易的Demo就在这儿了,我也是晚上花了1个小时试了下,感觉新浪SDK以及Example很nice,嘿嘿: )