通过 YouTube API 上传视频

mzh*_*huk 5 youtube api video youtube-api google-api-java-client

我正在使用 YouTube API 以编程方式将视频上传到 YouTube。我的一些视频需要标记年龄限制,所以我想指定 AgeGating 视频属性。当指定 video.setAgeGating(gating) 时,还必须提供适当的部分名称,否则我会收到以下错误

{
  "code" : 400,
  "errors" : [ {
    "domain" : "youtube.part",
    "location" : "part",
    "locationType" : "parameter",
    "message" : "ageGating",
    "reason" : "unexpectedPart"
  } ],
  "message" : "ageGating"
}
Run Code Online (Sandbox Code Playgroud)

文件指出以下可用的部分:

片段、内容详细信息、文件详细信息、实时流媒体详细信息、播放器、处理详细信息、录制详细信息、统计信息、状态、建议和主题详细信息。

在我的情况下,它们都不起作用,仍然返回相同的unexpectedPart错误消息,因此我尝试了自定义ageGating部件名称,尽管这次响应是:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "youtube.common",
    "message" : "Forbidden",
    "reason" : "forbidden"
  } ],
  "message" : "Forbidden"
}
Run Code Online (Sandbox Code Playgroud)

YouTube API 错误文档页面中未列出此错误类型。

这是我的代码示例:

Video videoMetadata = new Video();

// set status
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("public");
videoMetadata.setStatus(status);

// set metadata snippet
VideoSnippet snippet = new VideoSnippet();
snippet.setTitle("Test Upload");
snippet.setDescription("YouTube Data API V3");
List<String> tags = new ArrayList<String>();
tags.add("YouTube Data API V3");
tags.add("Test Upload");
snippet.setTags(tags);
videoMetadata.setSnippet(snippet);

// set video content
InputStreamContent videoContent = new InputStreamContent(
          VIDEO_FILE_FORMAT, new BufferedInputStream(new FileInputStream(videoFile)));
      videoContent.setLength(videoFile.length());

// set age gating
VideoAgeGating gating = new VideoAgeGating();
gating.setRestricted(true);
videoMetadata.setAgeGating(gating);


YouTube.Videos.Insert videoInsert = youtube.videos()
  .insert("ageGating,snippet,statistics,status", videoMetadata, videoContent);

Video returnedVideo = videoInsert.execute();
Run Code Online (Sandbox Code Playgroud)

是否禁止为新视频指定年龄限制,或者此案例是否有另一个视频部分名称?

Gon*_*nza 0

阅读文档似乎 youtube 年龄限制属性是另一个内容分级

如果我理解文档,您将无法通过 YouTube API 设置此属性。我读到您必须在请求正文中发布视频资源,并且只能设置视频资源的某些属性

您可以设置这些属性的值:

snippet.title
snippet.description
snippet.tags[]
snippet.categoryId
status.privacyStatus
status.embeddable
status.license
status.publicStatsViewable
status.publishAt
recordingDetails.locationDescription
recordingDetails.location.latitude
recordingDetails.location.longitude
recordingDetails.recordingDate
Run Code Online (Sandbox Code Playgroud)

您可以将此属性读取为:contentDetails.contentRating.ytRating = "ytAgeRestricted"但看起来您无法在对 Youtube API 的 POST 请求正文中的视频资源中 POST 此属性

    {
      ... 
      "contentDetails": {
        ...
        "contentRating": {
                "ytRating": "ytAgeRestricted"
            }
            ...
        }
    }
Run Code Online (Sandbox Code Playgroud)