2015-11-06 62 views
0

我正在尝试使用YouTube直播一些视频。我的想法是在我的应用程序中播放一些视频,如果需要,可以使用YouTube进行直播。运行YouTube数据API示例代码时未找到名称空间

现在,我只是试图运行一个简单的代码示例,上传视频。样品是从这个网址:https://developers.google.com/youtube/v3/code_samples/dotnet#upload_a_video

这是我的代码有:

Program.cs的

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     VideoStreamTest video = new VideoStreamTest(); 
    } 
} 

VideoStreamTest.cs

用下面的usings :

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; 

而我的等级:

class VideoStreamTest 
{ 
    public VideoStreamTest() 
    { 
     Console.WriteLine("=================\nTest Broadcast Youtube\n================="); 
     init(); 
    } 

    private static void init() 
    { 
     Console.WriteLine("YouTube Data API: Upload Video"); 
     Console.WriteLine("=============================="); 

     try 
     { 
      new UploadVideo().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 an application to upload files to the 
       // authenticated user's YouTube channel, but doesn't allow other types of access. 
       new[] { YouTubeService.Scope.YoutubeUpload }, 
       "user", 
       CancellationToken.None 
      ); 
     } 

     var youtubeService = new YouTubeService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
      ApplicationName = Assembly.GetExecutingAssembly().GetName().Name 
     }); 

     var video = new Video(); 
     video.Snippet = new VideoSnippet(); 
     video.Snippet.Title = "Default Video Title"; 
     video.Snippet.Description = "Default Video Description"; 
     video.Snippet.Tags = new string[] { "tag1", "tag2" }; 
     video.Snippet.CategoryId = "22"; // See https://developers.google.com/youtube/v3/docs/videoCategories/list 
     video.Status = new VideoStatus(); 
     video.Status.PrivacyStatus = "unlisted"; // or "private" or "public" 
     var filePath = @"REPLACE_ME.mp4"; // Replace with path to actual movie file. 

     using (var fileStream = new FileStream(filePath, FileMode.Open)) 
     { 
      var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, "video/*"); 
      videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged; 
      videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived; 

      await videosInsertRequest.UploadAsync(); 
     } 
    } 

    void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress) 
    { 
     switch (progress.Status) 
     { 
      case UploadStatus.Uploading: 
       Console.WriteLine("{0} bytes sent.", progress.BytesSent); 
       break; 

      case UploadStatus.Failed: 
       Console.WriteLine("An error prevented the upload from completing.\n{0}", progress.Exception); 
       break; 
     } 
    } 

    void videosInsertRequest_ResponseReceived(Video video) 
    { 
     Console.WriteLine("Video id '{0}' was successfully uploaded.", video.Id); 
    } 
} 

我已经安装了以下的NuGet包:

Google.Apis.YouTube.v3

Google.Apis

但是当我运行我的代码我有以下错误:

VideoStreamTest.cs(31,13,31,24): error CS0246: The type or namespace name 'UploadVideo' could not be found (are you missing a using directive or an assembly reference?) 

我在做什么错了?

回答

1

除了源代码文件中的“using”语句之外,您还必须在项目的References区域添加对API程序集的引用。

0

在我的情况下, 当我复制&贴'使用Google.Apis.YouTube.v3;'发生错误,但直接用键盘输入错误消失。