一、之前的調用方式
這種方式用了大約有一年時間,之前沒有問題,但是2019-8-28號突然不行了,可能是由於微博對該接口的調用做了限制。不允許通過傳遞source參數的方式進行請求
/** * source去新浪微博申請App Key * @param long_url * @return */ @Deprecated public static String getShortUrl2(String long_url) { String result = HttpUtils.get( "https://api.weibo.com/2/short_url/shorten.json?source="+app_key+"&url_long=" + long_url); JSONObject jsonObject = JSON.parseObject(result); JSONObject urls = jsonObject.getJSONArray("urls").getJSONObject(0); String url_short = urls.getString("url_short"); return url_short; }
二、修改后的調用方式
該接口微博API文檔地址
/** * source去新浪微博申請App Key * @param long_url * @return */ public static String getShortUrl(String long_url) { String result = HttpUtils.get( "https://api.t.sina.com.cn/short_url/shorten.json?source="+app_key+"&url_long=" + long_url); JSONObject jsonObject = JSON.parseArray(result).getJSONObject(0); String url_short = jsonObject.getString("url_short"); return url_short;