2014-09-10 83 views
1

我正在用C#与Web应用程序一起工作,用户将浏览视频文件,Web应用程序将通过YouTube API V3上传到YouTube。我收到错误。请咨询我在哪里做错了?YouTube API V3上传视频抛出异常

错误:System.ArgumentNullException:值不能为空。参数名称:Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)上的baseUri,Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务),Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务),Microsoft.Runtime .CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()at Google.Apis.Upload.ResumableUpload1.d__e.MoveNext()in c:\ code \ google.com \ google-api-dotnet-client \ default \ Tools \ Google.Apis。 Release \ bin \ Debug \ output \ default \ Src \ GoogleApis \ Apis [Media] \ Upload \ ResumableUpload.cs:line 459

我遵循以下几点。

  1. 创建了Web应用程序的ClientID,并从API Access页面下载了client_secrets.json文件。
  2. 使用https://developers.google.com/youtube/v3/docs/videos/insert#examples中提供的.Net示例代码也提到了相同的代码https://developers.google.com/youtube/v3/code_samples/dotnet
  3. 我的应用程序已获得YouTube API的授权。
  4. 上传视频文件时,出现错误。

我粘贴在我的代码下方供您参考。

/* * TestFileUploader.aspx/

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestFileUploader.aspx.cs" Inherits="TestFileUploader" Async="true" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
</head> 
<body> 
    <form id="qaform" runat="server"> 
     <div> 
      <p> 
       <asp:FileUpload runat="server" ID="FileUpload1" onchange="AddEventChoosePicture(this,'FileUpload1')" 
        Style="width: 100%;" /> 
      </p> 
      <p> 
       <asp:Button ID="submit" runat="server" OnClick="submit_Click" Text="Submit" /> 
      </p> 
     </div> 
    </form> 
</body> 
</html> 

/* * TestFileUploader.aspx.cs/

using System; 
using System.Web; 
using System.IO; 
using QA.credential; 

using Google.Apis.Auth.OAuth2; 
using Google.Apis.YouTube.v3; 
using Google.Apis.Services; 
using Google.Apis.YouTube.v3.Data; 
using Google.Apis.Upload; 

public partial class TestFileUploader : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 

    protected void submit_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      if (FileUpload1.HasFile) 
      { 
       String FileUpload1_Ext = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName); 
       UploadVideos(FileUpload1.FileContent, FileUpload1.PostedFile.ContentType); 
      } 
     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
     } 

    } 

    private async void UploadVideos(Stream uploadedStream, String contenttype) 
    { 
     try 
     { 
      UserCredential credential; 
      String clientsecretkeypath = HttpContext.Current.Server.MapPath("~/client_secrets.json"); 
      using (var stream = new FileStream(clientsecretkeypath, FileMode.Open, FileAccess.Read)) 
      { 
       credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets, 
        new[] { YouTubeService.Scope.YoutubeUpload }, 
        "user", System.Threading.CancellationToken.None); 
      } 

      // Create the service. 
      var youtubeService = new YouTubeService(new BaseClientService.Initializer() 
      { 
       //ApiKey = "AIzaSyAFxuAA4r4pf6VX75zEwCrIh5z4QkzOZ6M", 
       HttpClientInitializer = credential, 
       ApplicationName = PhotoandVideoupload.applicationname 
      }); 

      var video = new Video(); 
      video.Snippet = new VideoSnippet(); 
      video.Snippet.Title = "My Test Movie"; 
      video.Snippet.Description = "My description"; 
      video.Snippet.Tags = new string[] { "Autos" }; 
      video.Snippet.CategoryId = "2"; 
      video.Status = new VideoStatus(); 
      video.Status.PrivacyStatus = "unlisted"; 

      // Using snippet,status below throws 401(UnAuthorized issue). 
      // Using snippet alone throws 404(Bad Request). 
      //In both case, Null Exception throws for parameter baseURI. 
      var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet", uploadedStream, contenttype); 
      videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged; 
      videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived; 

      Google.Apis.Upload.IUploadProgress progress = await videosInsertRequest.UploadAsync(); 

      switch (progress.Status) 
      { 
       case UploadStatus.Uploading: 
        Response.Write(String.Format("{0} bytes sent.", progress.BytesSent)); 
        break; 

       case UploadStatus.Failed: 
        Response.Write(String.Format("{0}<br/>", progress.Exception.Message)); 
        Response.Write(String.Format("{0}<br/>", progress.Exception.StackTrace)); 
        break; 
      } 


      // Also tried to read file from server path instead of uploading via fileupload control. 
      /* 
      using (var fileStream = new FileStream(HttpContext.Current.Server.MapPath("~/1.mp4"), FileMode.Open)) 
      { 
       var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", fileStream, contenttype); 
       videosInsertRequest.ProgressChanged += videosInsertRequest_ProgressChanged; 
       videosInsertRequest.ResponseReceived += videosInsertRequest_ResponseReceived; 

       await videosInsertRequest.UploadAsync(); 
      } 
      */ 

     } 
     catch (Exception ex) 
     { 
      Response.Write(ex.Message + " " + ex.StackTrace); 
     } 
     finally 
     { 
     } 
    } 

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

      case UploadStatus.Failed: 
       Response.Write(String.Format("{0}<br/>", progress.Exception.Message)); 
       Response.Write(String.Format("{0}<br/>", progress.Exception.StackTrace)); 
       break; 
     } 
    } 

    void videosInsertRequest_ResponseReceived(Video video) 
    { 
     Response.Write(String.Format("Video id '{0}' was successfully uploaded.", video.Id)); 
    } 

} 

请指点我在哪里做错了吗?

感谢,

+0

您可能需要使用Fiddler来捕捉潜在的错误。当我有一个参数错误时,我遇到了这个错误。使用Fiddler记录IP数据包,我能够看到名为bad参数的底层错误。 Google API DotNet客户端[问题480](http://code.google.com/p/google-api-dotnet-client/issues/detail?id=480)要求增强API以返回包含潜在的错误。如果您认为将来可以帮助您进一步调试,请投票选出。 – 2014-09-11 22:15:52

+0

你是否设法通过这个错误? – briler 2014-10-06 07:18:06

+0

@Briler:还没有。如果您发现实际问题,请在此分享。 – DCI 2014-12-11 07:25:17

回答

0

你可以尝试以下变化:

来源:

var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet", uploadedStream, contenttype); 

要:

var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", uploadedStream, contenttype); 

此外,什么是contentType中?我使用video/*作为视频上传的内容类型。

状态码401错误可能是因为YouTube API尚未获得授权。检查您是否已授权YouTube API以获取您的凭证。转到Google Developers Console并点击您的项目名称。然后点击左侧导航菜单中的“API”。请查看YouTube Data API v3已打开。

+0

当我改变你的建议,我越来越“响应状态码不表示成功:401(未授权)。”和“值不能为空。参数名称:baseUri”。 – DCI 2014-09-12 06:36:41

+0

我正在阅读浏览文件的内容类型。我试着用.mp4文件。因此,在这种情况下,contenttype将作为视频/ mp4传递。 我也试图通过视频/ *,仍然是同样的问题。 – DCI 2014-09-12 06:37:50

+0

我修改了我的答案,以解决状态代码401错误的可能原因。 – 2014-09-12 12:18:34