2015-12-02 30 views
-1

我正在使用谷歌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'類型第一個參數的擴展方法'運行'(缺少使用指令或程序集引用?)

+3

我不確定你在這裏試圖做什麼,因爲它說,'Youtube_Retrieve_Uploads'中沒有'Run'方法,看起來你期望構造函數返回某種類型的任務?即使你可以得到這個編譯,它會拋出一個堆棧溢出異常... –

+2

看起來像代碼示例丟失運行方法...如果它確實是完整的[MCVE]比我看不出爲什麼你期望此代碼工作 - 請提供你的推理爲什麼你顯示的分類應該有'運行'方法。 –

回答

1

你忘了實現該方法public Task Run()的類Youtube_Retrieve_Uploads。別擔心:發生在我們身上。只是實現它,編譯器錯誤將消失:)

哦,還:Youtube_Retrieve_Uploads的構造函數是遞歸的。所以,如果你啓動你的程序,它會因堆棧溢出而崩潰。你可能也想解決這個問題。

+0

Abbondanza如何修復遞歸?什麼是最好的方式 –

+0

@SharondohpSharonas您的Youtube_Retrieve_Uploads構造函數創建另一個Youtube_Retrieve_Uploads對象,構造函數將創建另一個Youtube_Retrieve_Uploads對象等等。不要在構造函數中創建一個新的自身。 – sab669

1

您正試圖實例化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; 
     } 
     } 
    } 
    } 
} 
1

默認情況下,類將從「對象」類擴展。 Run()和Wait()不是該對象()類的一部分。您的課程Youtube_Retrieve_Uploads沒有顯示任何方法(在構造函數之外),並且它不擴展任何其他類。

該示例只能顯示該類的剪切,其中Run()和Wait()在別處定義。

相關問題