如何检索我的YouTube视频列表?我的程序挂起

时间:2015-09-13 10:11:45

标签: c# .net winforms youtube-api google-api-dotnet-client

由于某种原因,该程序挂起。 我正在使用Google API v3.0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;

namespace Test {
    class RetrieveYoutubeVideosList {
        private static int MaximumResults = 0;

        public static void RetrieveUploads(int MaxResults) {
            try {
                MaximumResults = MaxResults;
                new RetrieveYoutubeVideosList().Run().Wait();
            } catch (AggregateException ex) {
                foreach (var e in ex.InnerExceptions) {
                    Console.WriteLine("Error: " + e.Message);
                }
            }
        }

        UserCredential credential;
        string errr = "";
        private async Task Run() {
            try {
                using (var stream = new FileStream(@"C:\jason file\client_secrets.json", FileMode.Open, FileAccess.Read)) {
                    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        // This OAuth 2.0 access scope allows for read-only access to the authenticated 
                        // user's account, but not other types of account access.
                        new[] { YouTubeService.Scope.YoutubeReadonly },
                        "user",
                        CancellationToken.None,
                        new FileDataStore(this.GetType().ToString())
                    );
                }
            } catch(Exception err) {
                errr = err.ToString();
            }
            var youtubeService = new YouTubeService(new BaseClientService.Initializer() {
                HttpClientInitializer = credential,
                ApplicationName = this.GetType().ToString()
            });

            var channelsListRequest = youtubeService.Channels.List("contentDetails");
            channelsListRequest.Mine = true;

            // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
            var channelsListResponse = await channelsListRequest.ExecuteAsync();

            foreach (var channel in channelsListResponse.Items) {
                // From the API response, extract the playlist ID that identifies the list
                // of videos uploaded to the authenticated user's channel.
                var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;

                Console.WriteLine("Videos in list {0}", uploadsListId);

                var nextPageToken = "";
                while (nextPageToken != null) {
                    var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet");
                    playlistItemsListRequest.PlaylistId = uploadsListId;
                    playlistItemsListRequest.MaxResults = MaximumResults;
                    playlistItemsListRequest.PageToken = nextPageToken;

                    // Retrieve the list of videos uploaded to the authenticated user's channel.
                    var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();

                    foreach (var playlistItem in playlistItemsListResponse.Items) {
                        // Print information about each video.
                        Console.WriteLine("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId);
                    }

                    nextPageToken = playlistItemsListResponse.NextPageToken;
                }
            }
        }
    }
}

在form1的构造函数中:

RetrieveYoutubeVideosList.RetrieveUploads(50);

我正在使用断点。它走到了这一行:

var channelsListResponse = await channelsListRequest.ExecuteAsync();

但是一旦我点击继续没有任何反应。该程序一直挂起,直到我停止它。

0 个答案:

没有答案