2012-10-17 269 views

回答

31

第一步是越来越该用户的信道ID。我们可以通过请求Channels服务来做到这一点。这是一个JS例子。

var request = gapi.client.youtube.channels.list({ 
    // mine: true indicates that we want to retrieve the channel for the authenticated user. 
    mine: true, 
    part: 'contentDetails' 
}); 
request.execute(function(response) { 
    playlistId = response.result.channels[0].contentDetails.uploads; 
}); 

一旦我们得到的播放列表ID,我们可以用它来查询从PlaylistItems服务上传的视频列表。

var request = gapi.client.youtube.playlistItems.list({ 
    playlistId: playlistId, 
    part: 'snippet', 
}); 
request.execute(function(response) { 
    // Go through response.result.playlistItems to view list of uploaded videos. 
}); 
+0

对我来说'.list'方法不会返回。我有两个简单的日志,在调用方法之前和之后,第一个执行,第二个不执行。有任何想法吗? –

40

如果您使用的是客户端,那么格雷格的答案是正确的。要做到同样的事情与你进行以下2个请求基本要求:

  1. GET https://www.googleapis.com/youtube/v3/channels

    与参数:

    part=contentDetails 
    mine=true 
    key={YOUR_API_KEY} 
    

    和标题:

    ​​3210

    从这你会得到一个像这样的JSON响应:

    { 
    "kind": "youtube#channelListResponse", 
    "etag": "\"some-string\"", 
    "pageInfo": { 
        "totalResults": 1, 
        "resultsPerPage": 1 
    }, 
    "items": [ 
        { 
        "id": "some-id", 
        "kind": "youtube#channel", 
        "etag": "\"another-string\"", 
        "contentDetails": { 
        "relatedPlaylists": { 
        "likes": "channel-id-for-your-likes", 
        "favorites": "channel-id-for-your-favorites", 
        "uploads": "channel-id-for-your-uploads", 
        "watchHistory": "channel-id-for-your-watch-history", 
        "watchLater": "channel-id-for-your-watch-later" 
        } 
        } 
        } 
    ] 
    } 
    

    从这个要分析出来的“上传”通道ID。

  2. GET https://www.googleapis.com/youtube/v3/playlistItems

    与参数:

    part=snippet 
    maxResults=50 
    playlistId={YOUR_UPLOAD_PLAYLIST_ID} 
    key={YOUR_API_KEY} 
    

    和头:

    Authorization: Bearer {YOUR_TOKEN} 
    

    从此您将收到类似以下的JSON响应:

    { 
    "kind": "youtube#playlistItemListResponse", 
    "etag": "\"some-string\"", 
    "pageInfo": { 
        "totalResults": 1, 
        "resultsPerPage": 50 
    }, 
    "items": [ 
        { 
    
        "id": "some-id", 
        "kind": "youtube#playlistItem", 
        "etag": "\"another-string\"", 
        "snippet": { 
        "publishedAt": "some-date", 
        "channelId": "the-channel-id", 
        "title": "video-title", 
        "thumbnails": { 
        "default": { 
         "url": "thumbnail-address" 
        }, 
        "medium": { 
         "url": "thumbnail-address" 
        }, 
        "high": { 
         "url": "thumbnail-address" 
        } 
        }, 
        "playlistId": "upload-playlist-id", 
        "position": 0, 
        "resourceId": { 
        "kind": "youtube#video", 
        "videoId": "the-videos-id" 
        } 
        } 
        } 
    ] 
    } 
    

使用这种方法,你应该能够得到使用任何语言,甚至只是卷曲的信息。如果你想要比前50个结果更多,那么你将不得不使用第二个请求做多个查询并传入页面请求。更多关于这可以被读取在:http://developers.google.com/youtube/v3/docs/playlistItems/list

+0

你可以创建你在做什么的例子吗? –

+0

@PratikCJoshi使用https://developers.google.com/youtube/v3/docs/channels/list @底部查看演示。我通过:https://www.diigo.com/item/image/5enzo/a9ey产生“上传”:“UUhS0SPpEqGMGRim7mebedPg”。 @ https://developers.google.com/youtube/v3/docs/playlistItems/list我在做这件事:https://www.diigo.com/item/image/5enzo/up3b ...跛脚的事情没有包含内容长度。你可以通过contentDetails on/videos表达如“duration”:“PT7M18S”;我希望它没有包含这个疯狂的每个视频查询。 – kristopolous

+0

至于一个连贯的实现,我打算从V2升级到https://github.com/kristopolous/ytmix/blob/master/import/parse.js到v3(因为等等,我必须)。所以当你点击这个时,它会是v3。 – kristopolous

相关问题