2016-11-05 68 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) 
{ 
    //... 
}