您好老师,我现在使用的是一个同学在问题回答中推荐使用的xiaomark生成短链接,我生成了自己的apikey,但是在运行之后,报了403的错误,查了几天的解决方案,都不能解决,麻烦帮我看下如何解决,非常着急
代码如下:
package com.imooc.o2o.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class ShortNetAddressUtil {
static String actionUrl = "https://api.xiaomark.com/v1/link/create";
static String APIKEY = "34fa95f53dfce2e5bce3b141387183da";
public static void main(String[] args) {
String longUrl = "https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&announce_id=11615366683l3hgk&version=63010043&lang=zh_CN&token=";
System.out.println(getShortURL(longUrl));
}
@SuppressWarnings("deprecation")
public static String getShortURL(String longUrl) {
//longUrl = java.net.URLEncoder.encode(longUrl); xiaomark不需要处理原网址
//传好json数据
String apikey = APIKEY;
JSONObject param = new JSONObject();
param.put("apikey",apikey);
param.put("origin_url",longUrl);
//利用RestTemplate进行第三方接口的调用
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//设置超时时间30s
requestFactory.setConnectTimeout(30*1000);
requestFactory.setReadTimeout(30*1000);
RestTemplate restTemplate = new RestTemplate(requestFactory);
ResponseEntity<JSONObject> jsonObjectResponseEntity = restTemplate.postForEntity(actionUrl,param,JSONObject.class);
//将得到的json对象的body部分进行处理
JSONObject json = jsonObjectResponseEntity.getBody();
if (json == null) {
return "";
}
//读取多层嵌套的json数据
return json.getJSONObject("data").getJSONObject("link").getString("url");
}
}
错误如下:
Exception in thread “main” org.springframework.web.client.HttpClientErrorException$Forbidden: 403 FORBIDDEN: “{“code”:13,“data”:{},“message”:”\u8df3\u8f6c\u94fe\u63a5\u7684\u57df\u540d\u4e0d\u5728\u767d\u540d\u5355\u4e4b\u5185"}"
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:109)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:168)
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:819)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:777)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711)
at org.springframework.web.client.RestTemplate.postForEntity(RestTemplate.java:468)
SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需
了解课程