短链接 xiaomark 403错误

来源:14-11 店铺授权之短链接的实现

慕妹1025182

2022-04-23

您好老师,我现在使用的是一个同学在问题回答中推荐使用的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;

com.alibaba fastjson 1.2.45 **/ /** * * 使用xiaomark的短链接服务生成短链接 * */

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)

写回答

1回答

翔仔

2022-04-23

同学好,这里403是说没有权限去调用服务呢,短连接目前市面基本都封停了,可能是因为赚不到钱,可以尝试自己搭建一套,访问就可以了,其实主要就是生成一个对应于当前url的短网址,并将两者映射关系存储在数据库里,网上有很多短连接已经实现好的服务,拿来直接用也是可以的

https://gitee.com/m1603565290/url


如果实在不行,就只能严格按照xiaomark的文档去访问,应该是配置的key啥的匹配不上所以访问不到

https://xiaomark.com/book/open-api/group.html#%E8%AF%B7%E6%B1%82


0
5
翔仔
回复
慕妹1025182
是的,白名单里面要涉及到你访问的域名,不然没法通过,此外,还是建议同学完成课程之后自己搭建一套短链接服务,以防后续xiaomark也用不了了还能有自己的一套方案,顺便也学习下
2022-04-29
共5条回复

Java双版本(SSM到SpringBoot)校园商铺全栈开发

SSM商铺V1.0,解决毕设痛点;SpringBoot商铺V2.0,满足工作刚需

5125 学习 · 8148 问题

查看课程