Bitly使用指南

Bitly使用指南

1.概述

Bitly是世界上最广泛信任的链接管理平台。通过使用Bitly API,您将通过自动链接定制、移动深度链接和点击分析来充分发挥链接的功能。此文档主要讲述把长链接通过Bitly API转换成短链接,可通过访问生成的短链接直接访问原访问地址。主要分三步

2.准备

在此之前你需要到官网https://app.bitly.com/,注册账号(若已有账号,可跳过),

在这里插入图片描述
在这里插入图片描述
之后你会在邮箱收到一封验证邮件
在这里插入图片描述
然后回到登录页面登录即可

最后去获得一个token
在这里插入图片描述

3.生成短链

生成短链一共分为三步,最终的Util工具包和resp接收响应对象都放在文档最后

第一步,获取guid

https://dev.bitly.com/api-reference/#getGroups

public static String getGroupId() {
    String resp = HttpRequest.get(groupsUrl).
    //若jdk版本为1.7需要指定协议为1.2,jdk1.7默认为1.1,jdk1.8默认为1.2
    setSSLProtocol("TLSv1.2").
    header("Authorization", "Bearer " + BitlyConfig.accessToken).
    execute().
    body();
    logger.info("BitlyUtil_getGroupId_resp:{}", resp);

    BitlyGroupResp bitlyGroupResp = JSON.parseObject(resp, BitlyGroupResp.class);
    if (ObjectUtil.isNotNull(bitlyGroupResp)) {
        return bitlyGroupResp.getGroups().get(0).getGuid();
    }

    return null;
}
第二步,生成短链接

https://dev.bitly.com/api-reference/#createBitlink

Map<String, Object> params = new HashMap<>(3);
params.put("long_url", longUrl);
params.put("group_guid", groupId);
params.put("domain", domain);

String shortenResp = HttpRequest.post(shortenUrl).
        //若jdk版本为1.7需要指定协议为1.2,jdk1.7默认为1.1,jdk1.8默认为1.2
        setSSLProtocol("TLSv1.2").
        header("Authorization", "Bearer " + BitlyConfig.accessToken).
        header("Content-Type", "application/json").
        body(JSON.toJSONString(params)).
        execute().body();
logger.info("BitlyUtil_geBitlyShorten_resp:{}", resp);
BitlyShortenResp bitlyShortenResp = JSON.parseObject(shortenResp, BitlyShortenResp.class);

return bitlyShortenResp.getId();
第三步,自定义短链接

https://dev.bitly.com/api-reference/#addCustomBitlink

params.put("custom_bitlink", extend);
params.put("bitlink_id", bitlyShortenResp.getId());

String resp = HttpRequest.post(customBitlinksUrl).
        //若jdk版本为1.7需要指定协议为1.2,jdk1.7默认为1.1,jdk1.8默认为1.2
        setSSLProtocol("TLSv1.2").
        header("Authorization", "Bearer " + BitlyConfig.accessToken).
        header("Content-Type", "application/json").
        body(JSON.toJSONString(params)).
        execute().body();
logger.info("BitlyUtil_geBitlyShorten_customerResp:{}", resp);
CustomBitlinksResp customBitlinksResp = JSON.parseObject(resp, CustomBitlinksResp.class);

return customBitlinksResp.getCustom_bitlink();

4.工具类

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.bk.llh.demo02.config.BitlyConfig;
import com.bk.llh.demo02.core.resp.BitlyGroupResp;
import com.bk.llh.demo02.core.resp.BitlyShortenResp;
import com.bk.llh.demo02.core.resp.CustomBitlinksResp;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liulihao
 */
public class BitlyUtil {
    public static String groupsUrl = BitlyConfig.domain + "groups";
    public static String shortenUrl = BitlyConfig.domain + "shorten";
    public static String customBitlinksUrl = BitlyConfig.domain + "custom_bitlinks";

    /**
     * 1.getGroupId
     *
     * @return
     */
    public static String getGroupId() {
        String resp = HttpRequest.get(groupsUrl).
                //若jdk版本为1.7需要指定协议为1.2,jdk1.7默认为1.1,jdk1.8默认为1.2
                //setSSLProtocol("TLSv1.2").
                header("Authorization", "Bearer " + BitlyConfig.accessToken).
                execute().body();

        BitlyGroupResp bitlyGroupResp = JSON.parseObject(resp, BitlyGroupResp.class);
        if (ObjectUtil.isNotNull(bitlyGroupResp)) {
            return bitlyGroupResp.getGroups().get(0).getGuid();
        }

        return null;
    }

    /**
     * 2.getBitlyShorten
     *
     * @param longUrl 需要转短链的长链
     * @param domain
     * @param extend 自定义短链
     * @return
     */
    public static String getBitlyShorten(String longUrl, String domain, String extend) {
        //1.获取groupId
        String groupId = getGroupId();

        //2.生成短链接
        Map<String, Object> params = new HashMap<>(3);
        params.put("long_url", longUrl);
        params.put("group_guid", groupId);
        params.put("domain", domain);

        String shortenResp = HttpRequest.post(shortenUrl).
                //若jdk版本为1.7需要指定协议为1.2,jdk1.7默认为1.1,jdk1.8默认为1.2
                //setSSLProtocol("TLSv1.2").
                header("Authorization", "Bearer " + BitlyConfig.accessToken).
                header("Content-Type", "application/json").
                body(JSON.toJSONString(params)).
                execute().body();
        BitlyShortenResp bitlyShortenResp = JSON.parseObject(shortenResp, BitlyShortenResp.class);


        //3.自定义短链
        params.put("custom_bitlink", extend);
        params.put("bitlink_id", bitlyShortenResp.getId());

        String resp = HttpRequest.post(customBitlinksUrl).
                //若jdk版本为1.7需要指定协议为1.2,jdk1.7默认为1.1,jdk1.8默认为1.2
                //setSSLProtocol("TLSv1.2").
                header("Authorization", "Bearer " + BitlyConfig.accessToken).
                header("Content-Type", "application/json").
                body(JSON.toJSONString(params)).
                execute().body();
        CustomBitlinksResp customBitlinksResp = JSON.parseObject(resp, CustomBitlinksResp.class);

        return customBitlinksResp.getCustom_bitlink();
    }
}

5.接收响应对象

BitlyGroupResp

import java.util.List;

/**
 * @author liulihao
 */

public class BitlyGroupResp {
    private List<GroupsBean> groups;

    public static class GroupsBean {
        private String guid;

        public String getGuid() {
            return guid;
        }

        public void setGuid(String guid) {
            this.guid = guid;
        }
    }

    public List<GroupsBean> getGroups() {
        return groups;
    }

    public void setGroups(List<GroupsBean> groups) {
        this.groups = groups;
    }
}

BitlyShortenResp

public class BitlyShortenResp {
    private String id;
    private String link;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }
}

CustomBitlinksResp

public class CustomBitlinksResp {

    private String custom_bitlink;

    public String getCustom_bitlink() {
        return custom_bitlink;
    }

    public void setCustom_bitlink(String custom_bitlink) {
        this.custom_bitlink = custom_bitlink;
    }
}

6.配置类

import org.springframework.stereotype.Component;

/**
 * @author liulihao
 * @since 2022/9/13 9:35
 */
@Component
public class BitlyConfig {
    /**
     * api前缀
     */
    public static final String domain = "https://api-ssl.bitly.com/v4/";
    /**
     * 配置成自己的token
     */
    public static final String accessToken = "abcdefghijklmnpqrstuvwxyz123456789";
}





 



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘星星star

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值