为什么我在新类中出现错误:不包含“运行”的定义?
我正在使用谷歌api v3,我尝试使用这个例子,第一个说:检索我的上传。在这个例子中,他们使用Run和Wait。我想以这种方式尝试。 这是例子的顶部:为什么我在新类中出现错误:不包含“运行”的定义?
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 Google.Apis.YouTube.Samples
{
/// <summary>
/// YouTube Data API v3 sample: retrieve my uploads.
/// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
/// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
/// </summary>
internal class MyUploads
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("YouTube Data API: My Uploads");
Console.WriteLine("============================");
try
{
new MyUploads().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("Error: " + e.Message);
}
}
这是我的课我做了什么:
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
{
new Youtube_Retrieve_Uploads().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("Error: " + e.Message);
}
}
}
}
}
我得到的错误是:
错误1“Youtube.Youtube_Retrieve_Uploads '不包含'运行'的定义,并且没有找到接受'Youtube.Youtube_Retrieve_Uploads'类型第一个参数的扩展方法'运行'(缺少使用指令或程序集引用?)
你忘了实现该方法public Task Run()
的类Youtube_Retrieve_Uploads
。别担心:发生在我们身上。只是实现它,编译器错误将消失:)
哦,还:Youtube_Retrieve_Uploads
的构造函数是递归的。所以,如果你启动你的程序,它会因堆栈溢出而崩溃。你可能也想解决这个问题。
Abbondanza如何修复递归?什么是最好的方式 –
@SharondohpSharonas您的Youtube_Retrieve_Uploads构造函数创建另一个Youtube_Retrieve_Uploads对象,构造函数将创建另一个Youtube_Retrieve_Uploads对象等等。不要在构造函数中创建一个新的自身。 – sab669
您正试图实例化Youtube_Retrieve_Uploads
,但它不包含Run
方法。从docs试试这个例子:
/*
*/
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 Google.Apis.YouTube.Samples
{
/// <summary>
/// YouTube Data API v3 sample: retrieve my uploads.
/// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
/// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
/// </summary>
internal class MyUploads
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("YouTube Data API: My Uploads");
Console.WriteLine("============================");
try
{
new MyUploads().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("Error: " + e.Message);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("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())
);
}
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;
}
}
}
}
}
默认情况下,类将从“对象”类扩展。 Run()和Wait()不是该对象()类的一部分。您的课程Youtube_Retrieve_Uploads没有显示任何方法(在构造函数之外),并且它不扩展任何其他类。
该示例只能显示该类的剪切,其中Run()和Wait()在别处定义。
我不确定你在这里试图做什么,因为它说,'Youtube_Retrieve_Uploads'中没有'Run'方法,看起来你期望构造函数返回某种类型的任务?即使你可以得到这个编译,它会抛出一个堆栈溢出异常... –
看起来像代码示例丢失运行方法...如果它确实是完整的[MCVE]比我看不出为什么你期望此代码工作 - 请提供你的推理为什么你显示的分类应该有'运行'方法。 –