音乐第三方(腾讯正版曲库直通车)
官方文档:https://cloud.tencent.com/document/product/1155
腾讯曲库签名地址(id,key): https://cloud.tencent.com/document/api/213/30654
一、基本申请流程:
重点申请id和key地址:https://cloud.tencent.com/document/api/213/30654
切换子账号或继续使用,我点继续使用。
点击新建。
注意:新建后保存好之后会用。
回到API文档测试:https://cloud.tencent.com/document/product/1155/40100
填写自己创建的id and key 就可以测试了,因为没买曲,所以会没有什么数据。
二、SpringBoot接入曲库:
pom.xml
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<commons.version>3.11</commons.version>
<tencentcloud.version>3.1.217</tencentcloud.version>
<swagger2.version>2.9.2</swagger2.version>
<swagger-ui.sversion>1.9.6</swagger-ui.sversion>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 腾讯第三方全家桶 -->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>${tencentcloud.version}</version>
</dependency>
<!-- StringUtils -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger-ui.sversion}</version>
</dependency>
</dependencies>
application.yml
lanys:
music:
#id
secretId: 输入自己的Id
#key
secretKey: 输入自己的Key
#购买歌曲后会需要将歌曲绑定域名,我使用它默认测试的
endpoint: ame.tencentcloudapi.com
#swagger
swagger:
title: 牟某公司
description: 牟某公司
termsOfServiceUrl: http://eurasia.plus/swagger-ui.html
ContactName: xxx
ContactUrl: http://eurasia.plus/swagger-ui.html
ContactEmail: 1090613735@qq.com
version: 1.0
packages: com.example.music_boot.controller
server:
port: 8080
servlet:
context-path: /music
endpoint指域名,购买歌曲后会需要将歌曲绑定域名,我使用它默认测试的
Swagger配置
SwaggerProperties
@Data
@ConfigurationProperties("lanys.swagger")
public class SwaggerProperties {
private String title;
private String description;
private String termsOfServiceUrl;
private String ContactName;
private String ContactUrl;
private String ContactEmail;
private String version;
private String packages;
}
SwaggerConfig
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
@Bean
@ConditionalOnMissingBean
public SwaggerProperties swaggerProperties(){
return new SwaggerProperties();
}
@Bean
public Docket createRestApi(SwaggerProperties swaggerProperties){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo(swaggerProperties))
.enable(true)
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getPackages()))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
*
* @return
*/
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
Contact contact = new Contact(swaggerProperties.getContactName(), swaggerProperties.getContactUrl(),swaggerProperties.getContactEmail());
return new ApiInfoBuilder()
.title(swaggerProperties.getTitle())
//描述
.description(swaggerProperties.getDescription())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.contact(contact)
.version(swaggerProperties.getVersion())
.build();
}
}
Result(自定义返回格式)
Result
@ApiModel(value = "返回值基本信息")
@Data
public class Result implements Serializable {
private int code;
private Object data;
private String msg;
public Result(int code, String msg) {
this.code = code;
this.msg = msg;
}
public Result(int code, Object data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
public static Result success() {
return new Result(ResultEnums.SUCCESS.getCode(), ResultEnums.SUCCESS.getMsg());
}
public static Result success(String data) {
return new Result(ResultEnums.SUCCESS.getCode(), data, ResultEnums.SUCCESS.getMsg());
}
public static Result FAIL() {
return new Result(ResultEnums.SUCCESS.getCode(), ResultEnums.SUCCESS.getMsg());
}
public static Result FAIL(String data) {
return new Result(ResultEnums.SUCCESS.getCode(), data, ResultEnums.SUCCESS.getMsg());
}
public static Result EXCEPTION() {
return new Result(ResultEnums.SUCCESS.getCode(), ResultEnums.SUCCESS.getMsg());
}
public static Result EXCEPTION(String data) {
return new Result(ResultEnums.SUCCESS.getCode(), data, ResultEnums.SUCCESS.getMsg());
}
public Result put(Object data) {
this.data = data;
return this;
}
}
ResultEnums
@Getter
public enum ResultEnums {
SUCCESS(200,"成功")
,FAIL(500,"异常")
,EXCEPTION(500,"失败");
private int code;
private String msg;
ResultEnums(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
MusicController
@Api(tags = "music接口")
@RestController
@RequestMapping("v1/music")
public class MusicController {
@Autowired
private MusicFamilyMeals musicFamilyMeals;
@PostMapping("/describe_auth_info")
@ApiOperation("获取授权项目列表")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "limit", value = "页", dataType = "long"),
@ApiImplicitParam(paramType = "query", name = "offset", value = "行", dataType = "long")
})
public Result describeAuthInfo(Long limit, Long offset) {
if (limit == null || offset == null) {
return Result.success().put(musicFamilyMeals.DescribeAuthInfo());
}
return Result.success().put(musicFamilyMeals.DescribeAuthInfo(limit, offset));
}
@ApiOperation("获取授权项目已购云音乐列表")
@PostMapping("/describe_cloud_music")
public Result describeCloudMusicPurchased() {
return Result.success().put(musicFamilyMeals.DescribeCloudMusicPurchased());
}
@ApiOperation("获取云音乐播放信息")
@PostMapping("describe_cloud_music ")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "musicId", value = "歌曲Id", dataType = "String")
})
public Result describeCloudMusic(String musicId) {
if (StringUtils.isNotBlank(musicId)){
return Result.success().put(musicFamilyMeals.describeCloudMusic(musicId));
}
return Result.FAIL("数据出现小问题");
}
@ApiOperation("获取已购曲库包列表")
@PostMapping("/describe_packages")
public Result describePackages() {
return Result.success().put(musicFamilyMeals.describePackages());
}
@ApiOperation("获取曲库包歌曲播放信息")
@PostMapping("describe_music")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "itemId", value = "歌曲ID", dataType = "String"),
@ApiImplicitParam(paramType = "query", name = "offset", value = "在应用前端播放音乐C端用户的唯一标识。无需是账户信息,用户唯一标识即可", dataType = "String")
})
public Result describeMusic(String itemId, String identityId) {
return Result.success().put(musicFamilyMeals.describeMusic(itemId, identityId));
}
@ApiOperation("获取歌词信息")
@PostMapping("/describe_lyric")
@ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "itemId", value = "歌曲id", dataType = "String")})
public Result describeLyric(String itemId) {
return Result.success().put(musicFamilyMeals.describeLyric(itemId));
}
@ApiOperation("根据歌曲ID查询歌曲信息")
@PostMapping("/describe_Item_ById")
@ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "itemId", value = "根据歌曲ID查询歌曲信息", dataType = "String")})
public Result describeItemById(String itemId) {
return Result.success().put(musicFamilyMeals.describeItemById(itemId));
}
@ApiOperation("搜索即时广播歌曲")
@PostMapping("/search_ktv_musics")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "keyWord", value = "搜索关键词", dataType = "String"),
@ApiImplicitParam(paramType = "query", name = "limit", value = "页", dataType = "long"),
@ApiImplicitParam(paramType = "query", name = "offset", value = "行", dataType = "long")
})
public Result searchKTVMusics(String keyWord, Long limit, Long offset) {
return Result.success().put(musicFamilyMeals.searchKTVMusics(keyWord, limit, offset));
}
}
MusicFamilyMeals
@Slf4j
@Component
@NoArgsConstructor
public class MusicFamilyMeals {
@Value("${lanys.music.secretId}")
public String secretId;
@Value("${lanys.music.secretKey}")
public String secretKey;
@Value("${lanys.music.endpoint}")
public String endpoint;
/**
* 获取授权项目列表
*
* @return
*/
public String DescribeAuthInfo() {
try {
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeAuthInfoRequest req = new DescribeAuthInfoRequest();
DescribeAuthInfoResponse resp = client.DescribeAuthInfo(req);
return DescribeAuthInfoResponse.toJsonString(resp);
} catch (TencentCloudSDKException e) {
log.error("数据异常:" + e.toString());
}
return null;
}
/**
* 获取授权项目已购云音乐列表 分页
*
* @return
*/
public String DescribeAuthInfo(Long limit, Long offset) {
try {
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeAuthInfoRequest req = new DescribeAuthInfoRequest();
req.setLimit(limit);
req.setOffset(offset);
DescribeAuthInfoResponse resp = client.DescribeAuthInfo(req);
return DescribeAuthInfoResponse.toJsonString(resp);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 获取授权项目已购云音乐列表
*
* @return
*/
public String DescribeCloudMusicPurchased() {
try {
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeCloudMusicPurchasedRequest req = new DescribeCloudMusicPurchasedRequest();
/**
* 必加项目id
*/
req.setAuthInfoId("");
DescribeCloudMusicPurchasedResponse resp = client.DescribeCloudMusicPurchased(req);
System.out.println(DescribeCloudMusicPurchasedResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 获取云音乐播放信息
* @param musicId
* @return
*/
public String describeCloudMusic(String musicId){
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeCloudMusicRequest req = new DescribeCloudMusicRequest();
req.setMusicId(musicId);
DescribeCloudMusicResponse resp = client.DescribeCloudMusic(req);
System.out.println(DescribeCloudMusicResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 获取已购曲库包列表
* @return
*/
public String describePackages(){
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribePackagesRequest req = new DescribePackagesRequest();
DescribePackagesResponse resp = client.DescribePackages(req);
System.out.println(DescribePackagesResponse.toJsonString(resp));
return DescribePackagesResponse.toJsonString(resp);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 获取曲库包歌曲播放信息
* @param itemId 歌曲ID
* @param identityId 在应用前端播放音乐C端用户的唯一标识。无需是账户信息,用户唯一标识即可。
* @return
*/
public String describeMusic(String itemId,String identityId){
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeMusicRequest req = new DescribeMusicRequest();
req.setItemId(itemId);
req.setIdentityId(identityId);
DescribeMusicResponse resp = client.DescribeMusic(req);
System.out.println(DescribeMusicResponse.toJsonString(resp));
return DescribeMusicResponse.toJsonString(resp);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 获取歌词信息
* @param itemId 歌曲ID
* @return
*/
public String describeLyric(String itemId){
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeLyricRequest req = new DescribeLyricRequest();
//歌曲ID
req.setItemId(itemId);
DescribeLyricResponse resp = client.DescribeLyric(req);
System.out.println(DescribeLyricResponse.toJsonString(resp));
return DescribeLyricResponse.toJsonString(resp);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 根据歌曲ID查询歌曲信息
* @param itemID 歌曲ID,目前暂不支持批量查询
* @return
*/
public String describeItemById(String itemID ){
try{
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
DescribeItemByIdRequest req = new DescribeItemByIdRequest();
req.setItemIDs(itemID);
DescribeItemByIdResponse resp = client.DescribeItemById(req);
System.out.println(DescribeItemByIdResponse.toJsonString(resp));
return DescribeItemByIdResponse.toJsonString(resp);
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
return null;
}
/**
* 搜索即时广播歌曲
* @param keyWord 搜索关键词
* @param limit 分页游标
* @param offset 分页页长
* @return
*/
public String searchKTVMusics(String keyWord,Long limit,Long offset){
Credential cred = new Credential(secretId, secretKey);
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(endpoint);
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
AmeClient client = new AmeClient(cred, "", clientProfile);
//暂时没有该方法
// SearchKTVMusicsRequest req = new SearchKTVMusicsRequest();
//
// SearchKTVMusicsResponse resp = client.SearchKTVMusics(req);
//
// System.out.println(SearchKTVMusicsResponse.toJsonString(resp));
return null;
}
项目没有很好优化,访问 doc.html ,http://127.0.0.1:8080/music/doc.html
{
"code": 200,
"data": "{\"AuthInfo\":[],\"TotalCount\":0,\"RequestId\":\"70f3f7be-2905-4072-9fa1-b53418ba9b72\"}",
"msg": "成功"
}
总结
项目充当个人笔记,第一次接入第三方服务(一般接入第三方有很多坑的,例如广播曲库的类依赖中没有。还要自己没有真正的使用,对于向第三方获取数据没有做处理等),但是最后公司因为曲库太贵了,就没买,所以项目没做优化,充当笔记。