【问题标题】:How to check if a YouTube channel is active如何检查 YouTube 频道是否处于活动状态
【发布时间】:2013-06-29 23:11:49
【问题描述】:

无论用户是否创建了频道,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

对于经过身份验证的用户,该频道似乎存在,但在转到 YouTube 个人资料时,它指出“您必须创建一个频道才能上传视频。创建一个频道”,或者如果在用户未经过身份验证的情况下转到该 url,它'会说“此频道目前不可用。请稍后再试。”

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

【问题讨论】:

    标签: youtube youtube-api


    【解决方案1】:

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

    当您进行播放列表管理或视频上传等 API 调用时,如果没有链接频道,API 将抛出 GoogleJsonResponseException。下面的代码 sn-p 向您展示了当您尝试进行播放列表更新 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   
            }
         }
    }
    

    当您收到“youtubeSignupRequired”作为错误原因时,您会想要做一些事情。

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

    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
        }
    }
    

    【讨论】:

    • NP 亚伦。如果这是最佳答案,如果您将此问题设置为已回答,这将非常有帮助,这样当我在寻找帮助的人时它就不会出现在我的提要中!
    • 我也想要,但对我来说还不行。通过 Java 尝试和通过 api explorer 尝试时,我没有返回任何频道,状态永远不会填充。
    • 我能够在尝试为经过身份验证的用户获取频道提供者时重现“youTubeSignupRequired”异常。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-01-11
    • 2019-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    相关资源
    最近更新 更多