public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
String code = request.getParameter("code");
System.out.println(code+"-------------------code");
// Map<String, String> token = this.getAccessTokenAndUid(code);
//System.out.println("username-------------"+user.getName());
return "success";
}
private Map<String , String> getAccessTokenAndUid(String code){
String responseDate = "" ;
Map<String , String> token = new HashMap<String, String>();
//本机运行时会报证书错误
/*ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));*/
PostMethod postMethod = new PostMethod("https://api.weibo.com/oauth2/access_token");
postMethod.addParameter("grant_type", "authorization_code");
postMethod.addParameter("code",code);
postMethod.addParameter("redirect_uri","callBackURL");
postMethod.addParameter("client_id",Weibo.CONSUMER_KEY);
postMethod.addParameter("client_secret",Weibo.CONSUMER_SECRET);
HttpClient client = new HttpClient();
try {
client.executeMethod(postMethod);
responseDate = postMethod.getResponseBodyAsString();
} catch (Exception e) {
e.printStackTrace();
}
if(!responseDate.equals("") && responseDate.indexOf("access_token") != -1){
// JSONObject jsonData = JSONObject.fromObject(responseDate);
// token.put("accessToken", (String)jsonData.get("access_token"));
// token.put("uid", jsonData.getString("uid"));
}
return token;
}
http://my.oschina.net/u/873047/blog/102950
登录网站例子:http://www.hackhome.com/InfoView/Article_233790.html
http://my.oschina.net/u/873047/blog/102950 获取用户信息
rivate Map<String , String> getUserWeiBoInfo(Map<String , String> token){
10
Map<String , String> userData = new HashMap<String, String>();
11
String UserInfo = "";
12
String url = "https://api.weibo.com/2/users/show.json?access_token="+token.get("sinaUid")"&uid="+token.get("sinaAccessToken");
13
GetMethod getMethod = new GetMethod(url);
14
HttpClient client = new HttpClient();
15
try {
16
client.executeMethod(getMethod);
17
UserInfo = getMethod.getResponseBodyAsString();
18
JSONObject jsonData = JSONObject.fromObject(UserInfo);
19
userData.put("name",jsonData.getString("name").toString() );
20
userData.put("headImg", jsonData.getString("profile_image_url"));
21
} catch (Exception e) {
22
e.printStackTrace();
23
}
24
return userData;
25
}
在授权用户中发表一篇微博:
01
public String weiboLogin() {
02
HttpServletRequest request = ServletActionContext.getRequest();
03
String code = request.getParameter("code");
04
Map<String, String> token = this.getAccessTokenAndUid(code);
05
Map<String , String> userInfo = this.getUserWeiBoInfo(token);
06
this.shareToSina(token);
07
}
08
private void shareToSina(Map<String , String>) throws IllegalArgumentException, HttpException, IOException
09
{
10
/*ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
11
Protocol.registerProtocol("https", new Protocol("https", fcty, 443));*/
12
PostMethod postMethod = new PostMethod("https://api.weibo.com/2/statuses/update.json");
13
postMethod.addParameter("access_token", token.get("sinaAccessToken"));
14
postMethod.addParameter("status","发表一条微博");
15
HttpMethodParams param = postMethod.getParams();
16
param.setContentCharset("UTF-8");
17
HttpClient client = new HttpClient();
18
client.executeMethod(postMethod);
19
postMethod.getResponseBodyAsString();
20
}