使用YouTube API V3将视频插入播放列表

13

嘿,我对YouTube API相当熟悉...我知道如何阅读频道和播放列表,以及获取任何我认为重要的相关视频信息。我正在使用PHP来完成所有这些操作,但为了确保我正确理解每个请求/响应如何工作,我尝试在API资源管理器中进行。

现在,我正在尝试将视频插入我刚创建的播放列表(也是通过API),但我遇到了一些问题,无法确定我的请求格式是否正确。

这是请求:

POST https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&key={YOUR_API_KEY}

Content-Type:  application/json
Authorization:  Bearer ya29.1.AADtN_WT2TRZzH1t86nVlX26z9WPp-gnDTxVHGvdQ6xx0vyTzmkYeXkLdJerwllLzF_a
X-JavaScript-User-Agent:  Google APIs Explorer

{
  "snippet": {
    "playlistId": "PL8hD12HFC-nuswc21_e64aPAy9B25sEH7",
    "resourceId": {
      "videoId": "KMGuyGY5gvY"
    }
  }
} 

这是回应的内容

400 Bad Request

- Show headers -

{
 "error": {
  "errors": [
   {
     "domain": "youtube.playlistItem",
     "reason": "invalidResourceType",
     "message": "Resource type not supported."
   }
  ],
   "code": 400,
   "message": "Resource type not supported."
 }
}

播放列表是我使用API Explorer创建的一个空的公开可见的播放列表,并且可以通过标准的YouTube网站查看。视频是从YouTube上随机选择的...我尝试了几个视频,因为我认为可能是与某个特定视频有关的问题,但没有运气.. 我已经打开了OAuth并使用了一个验证过的YouTube合作伙伴帐户。

然而,我似乎无法弄清楚为什么不支持资源类型,我在网上搜索了很多答案,但没有找到完全符合我所做的错误...如果有人能帮助我解决这个问题,我将非常感激,因为我现在有些困扰。


你从哪里获取API密钥? - Rohan
请在此处查看已翻译的内容:https://developers.google.com/youtube/android/player/register - brendosthoughts
3个回答

23

您的 resourceId 不完整,因为它没有告诉 API 如何解释 videoId 参数。尝试设置 resourceIdkind 属性,就像这样:

  "snippet": {
    "playlistId": "PL8hD12HFC-nuswc21_e64aPAy9B25sEH7",
    "resourceId": {
      "kind": "youtube#video",
      "videoId": "KMGuyGY5gvY"
    }
  }

这样,API 就会知道在哪个“域”(可以这么说)中定位由您发送的字符串标识的资源。


3
太棒了,谢谢!如果我需要将一种类型定义为youtube#video,那么还有哪些可接受的类型?因为我认为只有youtube视频可以添加到播放列表中。 - brendosthoughts

3

这将有助于构建资源 ID

def add_video_to_playlist(videoID,playlistID):
    youtube = get_authenticated_service() #write it yourself
    add_video_request=youtube.playlistItems().insert(
        part="snippet",
        body={
                'snippet': {
                  'playlistId': playlistID, 
                  'resourceId': {
                          'kind': 'youtube#video',
                      'videoId': videoID
                    }
                #'position': 0
                }
        }
    ).execute()

2

请阅读Youtube数据API文档。

    $playlistId = 'REPLACE_WITH_YOUR_PLAYLIST_ID_STARTS_WITH_PL';
    $resourceId = new Google_Service_YouTube_ResourceId();
    $resourceId->setVideoId('REPLACE_WITH_VIDEO_ID_YOU_WANT_TO_ADD');
    $resourceId->setKind('youtube#video');


    $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
    $playlistItemSnippet->setTitle('YOUR_VIDEO_TITLE');
    $playlistItemSnippet->setPlaylistId($playlistId);
    $playlistItemSnippet->setResourceId($resourceId);


    $playlistItem = new Google_Service_YouTube_PlaylistItem();
    $playlistItem->setSnippet($playlistItemSnippet);
    $playlistItemResponse = $youtube->playlistItems->insert(
        'snippet,contentDetails', $playlistItem, array());</i>

完整项目可在我的GitHub上找到。


更多有关于PlaylistItems的相关链接: https://developers.google.com/youtube/v3/docs/playlistItems - Jeff Blumenthal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接