2015-06-07 85 views
-2

我需要一個Java方法,該方法向我提供了一個列表,其中列出了在我自己的YouTube頻道上上傳的所有YouTube觀看網址(或視頻ID)。由於私人和不公開的視頻應該包含在內,因此必須通過用戶名和密碼或OAuth 2.0進行身份驗證。獲取所有上傳的視頻

此外,我知道YouTube API,但我仍然不知道如何做到這一點。任何工作代碼片段,例如沒有像here這樣的模糊描述,因爲當它必須被自動化時它沒有用處。

基本上,我需要一個像這樣的方法:

public static List<String> getAllUploadedVideos(String channelUrl) 
{ 
    // use OAuth 2.0 to gain viewing permissions for the YouTube account 
    // get all uploaded videos 
} 

Here一個可以做的搜索查詢。

+1

對不起,StackOverflow不是一個代碼寫入服務。這是一個向人們詢問編寫自己的代碼時遇到的問題的地方。 – RealSkeptic

回答

0
public static List<String> getUploadedVideoIds() throws IOException 
{ 
    YouTube youtube; 
    List<String> scopes = Lists 
      .newArrayList("https://www.googleapis.com/auth/youtube.readonly"); 
    Credential credential = Auth.authorize(scopes, "myuploads"); 

    youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, 
      credential).setApplicationName(
      "youtube-cmdline-myuploads-sample").build(); 

    YouTube.Channels.List channelRequest = youtube.channels().list(
      "contentDetails"); 
    channelRequest.setMine(true); 
    channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo"); 
    ChannelListResponse channelResult = channelRequest.execute(); 

    List<Channel> channelsList = channelResult.getItems(); 

    String uploadPlaylistId = channelsList.get(0).getContentDetails() 
      .getRelatedPlaylists().getUploads(); 

    List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>(); 

    YouTube.PlaylistItems.List playlistItemRequest = youtube 
      .playlistItems().list("id,contentDetails,snippet"); 
    playlistItemRequest.setPlaylistId(uploadPlaylistId); 

    playlistItemRequest 
      .setFields("items(contentDetails/videoId),nextPageToken,pageInfo"); 

    String nextToken = ""; 

    do 
    { 
     playlistItemRequest.setPageToken(nextToken); 
     PlaylistItemListResponse playlistItemResult = playlistItemRequest 
       .execute(); 

     playlistItemList.addAll(playlistItemResult.getItems()); 

     nextToken = playlistItemResult.getNextPageToken(); 
    } while (nextToken != null); 

    List<String> videoIds = new ArrayList<>(); 

    for (PlaylistItem playListItem : playlistItemList) 
    { 
     videoIds.add(playListItem.getContentDetails().getVideoId()); 
    } 

    return videoIds; 
} 

基於YouTube API和Jeremy Walker的sample code

相關問題