当尝试使用谷歌 api v3 检索我的 youtube 视频上传列表时,请求后没有继续?



程序在尝试发出请求后挂起,永远不会继续。尝试使用try和ctach,但没有抛出任何异常或错误。

这是我在一个新类中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
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 Youtube
{

    class Youtube_Retrieve_Uploads
    {
        public Youtube_Retrieve_Uploads()
        {
            try
            {
                Run().Wait();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }
        }
        private async Task Run()
        {
            UserCredential credential;
            using (var stream = new FileStream(@"C:jason fileclient_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())
                );
            }
            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 = 50;
                    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;
                }
            }
        }
    }
}

我用了一个断点,当它在做这条线时:

var channelsListResponse = await channelsListRequest.ExecuteAsync();

它只是挂起了程序永远不会继续。

我承认这不是理想的解决方案,而是可能的解决方案之一,但在我的情况下,我删除了"wait"和*Async()关键字,现在它可以工作了。

var channelsListResponse = channelsListRequest.Execute();

而不是

var channelsListResponse = await channelsListRequest.ExecuteAsync();

var playlistItemsListResponse = playlistItemsListRequest.Execute();

而不是

var playlistItemsListResponse = await playlistItemsListRequest.ExecuteAsync();

相关内容

  • 没有找到相关文章

最新更新