gistfile1.txt import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;im
import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.developerworld.commons.command.OrderCommand; import org.developerworld.commons.command.PageCommand; import org.developerworld.commons.command.ResultCommand; import org.developerworld.commons.beanutils.BeanUtils; import org.developerworld.commons.lang.OperationException; import org.developerworld.commons.servlet.http.RequestUtils; import org.developerworld.frameworks.webui.datagrid.DataGrid; import weibo4j.http.AccessToken; import weibo4j.http.HttpClient; import weibo4j.model.PostParameter; import weibo4j.model.WeiboException; import weibo4j.util.WeiboConfig; @Controller("/wb/WbapiController") @RequestMapping("/wb/api") public class WbapiController { @Resource(name = "BusinessService") private WbapiBusinessService businessService; /** * 存储code * * @param response * @return */ @RequestMapping(value = "/setcode", method = RequestMethod.GET) @ResponseBody public MapsetCode(HttpServletRequest request, HttpServletResponse response) { Map map = new HashMap (); boolean flag = false; RequestUtils requestUtils = new RequestUtils(request); Map requestMap = requestUtils.toSmartMap(); String code = (String) requestMap.get("code"); try { MonitorWbapi monitorWbapi = businessService.getMonitorWbapiById(1); monitorWbapi.setCode(code); businessService.updateMonitorWbapi(1, monitorWbapi); flag = true; map.put("flag", flag); map.put("massage", getMessage(getMessage("prj.message.successful"))); } catch (Exception e) { map.put("flag", flag); map.put("msg", e.getMessage()); } return map; } /** * 获取code * * @param request * @param response * @return */ @RequestMapping(value = "/getcode", method = RequestMethod.GET) @ResponseBody public Map getCodes(HttpServletRequest request, HttpServletResponse response) { Map map = new HashMap (); boolean flag = false; try { MonitorWbapi monitorWbapi = businessService.getMonitorWbapiById(1); String code = monitorWbapi.getCode(); flag = true; map.put("flag", flag); map.put("code", code); } catch (Exception e) { map.put("flag", flag); } return map; } /** * 获取回调页 * * @param gzgitoken * @return * @throws WeiboException */ @RequestMapping(value = "/authorize", method = RequestMethod.GET) @ResponseBody public String authorize() throws WeiboException { String url = ""; try { MonitorWbapi monitorWbapi = businessService.getMonitorWbapiById(1); url = "https:// api.weibo.com/oauth2/authorize?client_id = " + monitorWbapi.getClientId() + "&redirect_uri=" + monitorWbapi.getRedirectUri() + ""; } catch (Exception e) { e.getMessage(); } return url; } /** * 根据code获取accesstoken token存活时间为5年,故一般不重新获取 * * @param request * @param code * @return * @throws WeiboException */ @RequestMapping(value = "/getAccessTokenByCode", method = RequestMethod.GET) @ResponseBody public AccessToken getAccessTokenByCode(HttpServletRequest request, String code) throws WeiboException { HttpClient client = new HttpClient(); Utils utils = new Utils(); return new AccessToken(client.post(WeiboConfig.getValue("accessTokenURL"), new PostParameter[]{new PostParameter("client_id", utils.getClient_id()), new PostParameter("client_secret", utils.getClient_secret()), new PostParameter("grant_type", "authorization_code"), new PostParameter("code", code), new PostParameter("redirect_uri", utils.getRedirect_uri())}, false, null)); } }