如何检查YouTube频道是否有效

App*_*les 1 youtube youtube-api

无论用户是否创建了频道,它都会显示,youtube api将返回该特定用户的频道.

Java API

YouTube.Channels.List search = youTube.get().channels().list("id);
search.setPart("id");
ChannelListResponse res = search.execute();
List<Channel> searchResultList = search.getItems()
Channel channel = searchResultList.get(0); // there is always a channel
Run Code Online (Sandbox Code Playgroud)

对于经过身份验证的用户,该频道似乎存在,但在转到YouTube个人资料时,它会指出"您必须创建一个频道来上传视频.创建一个频道",或者如果没有用户进行身份验证就转到该网址,它会说"此频道暂时无法使用.请稍后再试."

如何检查youtube频道是否处于活动状态.我是否必须尝试上传到它?

小智 5

有两种方法可以做到这一点:

当您进行API调用(例如播放列表管理或视频上传)时,如果没有链接频道,API将抛出GoogleJsonResponseException.这是一段代码片段,向您展示当您尝试进行播放列表更新API调用并且没有频道时会发生什么:

try {
    yt.playlistItems().insert("snippet,contentDetails", playlistItem).execute();
} catch (GoogleJsonResponseException e) {
    GoogleJsonError error = e.getDetails();
    for(GoogleJsonError.ErrorInfo errorInfo : error.getErrors()) {
        if(errorInfo.getReason().equals("youtubeSignupRequired")) {
        // Ask the user to create a channel and link their profile   
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

当你将"youtubeSignupRequired"作为错误原因时,你会想要做些什么.

另一种方法是提前检查.进行Channel.List调用并检查"items/status".您正在寻找布尔值"isLinked"等于"true".请注意,我在此示例代码中插入了一个强制转换,因为在此示例的版本中,客户端返回的是String值而不是类型化的Boolean:

YouTube.Channels.List channelRequest = youtube.channels().list("status");
channelRequest.setMine("true");
channelRequest.setFields("items/status");
ChannelListResponse channelResult = channelRequest.execute();
List<Channel> channelsList = channelResult.getItems();
for (Channel channel : channelsList) {
    Map<String, Object> status = (Map<String, Object>) channel.get("status");
    if (true == (Boolean) status.get("isLinked")) {
        // Channel is linked to a Google Account
    } else {
        // Channel is NOT linked to a Google Account
    }
}
Run Code Online (Sandbox Code Playgroud)