2016-11-05 82 views
-1

我试图创建一个网站,您可以通过它从设备上传视频到YouTube,但能够查看您从我的网站上载的那些特定视频(还有更多目的)Youtube api upload video.net

即时通讯使用.net C#和我有我的oauth密钥和开发人员的API键,下载JSON文件等。我下载了谷歌示例代码,但似乎无法得到它的工作,不完全确定它如何工作。

有人可以帮我解决这个问题吗?

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; 

/// <summary> 
/// Summary description for Video_Upload 
/// </summary> 
public class Video_Upload 
{ 
    public Video_Upload() 
    { 
    // 
    // TODO: Add constructor logic here 

    new Video_Upload().Run().Wait(); 

} 

private async Task Run() 
{ 
    UserCredential credential; 
    using (var stream = new FileStream("Client_id_googleApi.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 
    }); 

    //VIDEO INFO AND DETAILS 

    var video = new Video(); 
    video.Snippet = new VideoSnippet(); 
    video.Snippet.Title = "Test Video 1"; 
    video.Snippet.Description = "Testing Video Upload"; 
    video.Snippet.Tags = new string[] { "Test", "First" }; 
    video.Snippet.CategoryId = "17";//category id for sport // See https://developers.google.com/youtube/v3/docs/videoCategories/list 
    video.Snippet.ChannelId = "UCfvR-wqeoHmAGrHnoQRfs9w"; 
    video.Status = new VideoStatus(); 
    video.Status.PrivacyStatus = "public"; // or "private" or "public" 
    var filePath = @"C:\Users\siobhan\Documents\Visual Studio 2015\WebSites\FYP_November\GP010149.avi"; // 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(progress.BytesSent, " bytes sent."); 
       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); 
    } 


    } 



} 
+0

不是没有*关于您正在运行什么代码或什么是不工作的任何*信息,没有。无论你在屏幕上指出什么,我们都看不到它。 – David

+0

不习惯使用此网站,已上传代码 – shove195

+1

关于此代码的具体问题是什么?它以什么方式失败?当你调试这个时,它在哪里/具体是否失败? – David

回答

1

它说 '无效不能以这种方式使用'

因为你想声明的另一种方法里面的方法:

private async Task Run() 
{ 
    //... 

    void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress) 
    { 
     //... 
    } 

    //... 
} 

这ISN” t在C#中有效。方法在类中声明,并且基本上彼此并排。如:

private async Task Run() 
{ 
    //... 
} 

void videosInsertRequest_ProgressChanged(Google.Apis.Upload.IUploadProgress progress) 
{ 
    //... 
}