welcome to xlongwei.com

欢迎大家一起学习、交流、分享


QQ:9167702333 邮箱:admin@xlongwei.com

微博短链接接口


分类 Java   关键字 分享   标签 java   web   jquery   发布 hongwei  1444986473873
注意 转载须保留原文链接,译文链接,作者译者等信息。  
微博短链接接口支持source或access_token两种方式,这里介绍access_token的方式,demo页面,http://cms.xlongwei.com/demo/share.html

使用access_token的方式需要通过微博的OAuth2认证,
  1. authorise,授权页面,需要输入账户信息,回调地址返回code值
  2. access_token,使用code值,再提供client_secret等私密信息,返回access_token和有效期等
  3. shortenurl,使用access_token,返回url_short短链接

1,获得授权之后的code值,这里容易报错:”你所访问的站点在微博的认证失败“,redirect_uri要符合回调域名,值要转码
https://api.weibo.com/oauth2/authorize?client_id=2782844817&redirect_uri=http%3A%2F%2Fbbs.xlongwei.com%2Fcallback%2Fweibo.json
结果:{"access_token":"2.00VQKSnBpDX1CDef4c80af2d0el4Rp","expires_in":157679999,"remind_in":"157679999","uid":"1644502663"}

2,获得access_token,
@ResponseBody
@RequestMapping("weibo")
public JSONObject weibo(HttpServletRequest request) {
logger.info(RequestUtil.stringMap(request).toString());
String code = RequestUtil.getStringParam(request, "code", null);
String access_token = RequestUtil.getStringParam(request, "access_token", null);
if(code!=null) {//authorise callback,授权回调地址,也可以手动触发
Map<String, String> params = new HashMap<>();
params.put("client_id", GlobalConfig.getProperty("weibo.client_id"));
params.put("client_secret", GlobalConfig.getProperty("weibo.client_secret"));
params.put("grant_type", "authorization_code");
params.put("code", code); //第一步获得的code值
params.put("redirect_uri", "http://bbs.xlongwei.com/callback/weibo.json");
JSONObject json = ServiceRequester.request("https://api.weibo.com/oauth2/access_token", params);
//{"access_token":"2.00VQKSnBpDX1CDef4c80af2d0el4Rp","expires_in":157679999,"remind_in":"157679999","uid":"1644502663"}
access_token=json.getString("access_token");

GlobalConfig.setProperty("weibo.access_token", access_token);
request.getServletContext().setAttribute("weibo_access_token", true); //标记已获得access_token值,vm页面可以访问到它
return json;
}else if(access_token!=null) {//set access_token,可以直接植入/callback/weibo.json?access_token=access_token 值
GlobalConfig.setProperty("weibo.access_token", access_token);
request.getServletContext().setAttribute("weibo_access_token", true);
}
return null;
}

3,获得shorten_url,
function shortenUrl(btn){
var url=$("#shortenUrl").val();
if(url && $("#shortenCfx").is(":checked")) url = "http://www.caifuxiang.com/statics/redirect.html?by1067&"+url;
if(url && url.indexOf("http")==0){
if(!"$!weibo_access_token"){ //如果没有access_token,就只有浏览器打开以下链接了
url = encodeURIComponent(url);
url = "https://api.weibo.com/2/short_url/shorten.json?source=2782844817&url_long="+url;
$("#shortenLink").text(url).attr("href", url); //这个url点击打开后,结果是json串,需要手动复制url_short
}else{ //如果有access_token,就可以提交到后台调shorten_url接口获得url_short了。由于js的同源策略限制,所以无法直接post微博的接口
jQuery.post("$!front_url/demo/share.json", {url_long: url}, function(data){
url = data.urls&&data.urls.length>0 ? data.urls[0].url_short : "";
$("#shortenLink").text(url).attr("href", url); //这个url已经是url_short了,可以点一下验证
});
}
}
}
@ResponseBody
@RequestMapping(value="share",method=RequestMethod.POST)
public JSONObject share(HttpServletRequest request) { //这里提供ajax访问支持,调用微博的shorten_url接口,原样返回
String url_long = RequestUtil.getStringParam(request, "url_long", null);
if(url_long!=null && StringUtil.isUrl(url_long)) {
String access_token = GlobalConfig.getProperty("weibo.access_token");
if(StringUtil.hasLength(access_token)) {
Map<String, String> params = ServiceRequester.params();
params.put("access_token", access_token);
params.put("url_long", url_long);
JSONObject json = ServiceRequester.request("https://api.weibo.com/2/short_url/shorten.json", params);
logger.info(url_long+", url_short: "+json.getString("url_short"));
return json;
}
}
return null;
}

这个demo是为方便推广财富箱的项目而做的,选中复选按钮后就可以直接输入项目ID生成短链接,否则就需要输入完整的URL链接。