Youtube api YouTube API V3上传视频引发异常
我正在使用C#处理Web应用程序,用户将浏览视频文件,Web应用程序将通过YouTube API V3上传到YouTube。我得到了错误。请告诉我哪里做错了 错误:System.ArgumentNullException:值不能为null。参数名称:Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(任务任务)在Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(任务任务任务)在Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable1.ConfiguredTaskAwaiter.GetResult()位于c:\code\Google.com\Google api dotnet client\default\Tools\Google.api.Release\bin\Debug\output\default\Src\GoogleApis\api[Media]\Upload\ResumableUpload.cs:第459行 我遵循以下几点Youtube api YouTube API V3上传视频引发异常,youtube-api,Youtube Api,我正在使用C#处理Web应用程序,用户将浏览视频文件,Web应用程序将通过YouTube API V3上传到YouTube。我得到了错误。请告诉我哪里做错了 错误:System.ArgumentNullException:值不能为null。参数名称:Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)在Microsoft.Runtime.CompilerServices.TaskAwaiter.Handle
<%@ 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));
}
}
使用系统;
使用System.Web;
使用System.IO;
使用质量保证证书;
使用Google.api.Auth.OAuth2;
使用Google.api.YouTube.v3;
使用Google.api.Services;
使用Google.api.YouTube.v3.Data;
使用Google.api.Upload;
公共部分类TestFileUploader:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效提交\u单击(对象发送者,事件参数e)
{
尝试
{
if(FileUpload1.HasFile)
{
字符串FileUpload1_Ext=System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName);
上传视频(FileUpload1.FileContent,FileUpload1.PostedFile.ContentType);
}
}
捕获(例外情况除外)
{
响应。写入(例如消息);
}
}
私有异步void UploadVideos(流uploadedStream,字符串contenttype)
{
尝试
{
用户凭证;
字符串clientsecretkeypath=HttpContext.Current.Server.MapPath(“~/client\u secrets.json”);
使用(var stream=newfilestream(clientsecretkeypath,FileMode.Open,FileAccess.Read))
{
凭证=等待GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
新[]{YouTubeService.Scope.YoutubeUpload},
“用户”,System.Threading.CancellationToken.None);
}
//创建服务。
var youtubeService=new youtubeService(new BaseClientService.Initializer()
{
//ApiKey=“aizasyafxuaaa4r4pf6vx75zewcrih5z4qkzoz6m”,
HttpClientInitializer=凭证,
ApplicationName=PhotoandVideoupload.ApplicationName
});
var video=新视频();
video.Snippet=新的VideoSnippet();
video.Snippet.Title=“我的测试电影”;
video.Snippet.Description=“我的描述”;
video.Snippet.Tags=新字符串[]{“Autos”};
video.Snippet.CategoryId=“2”;
video.Status=新的VideoStatus();
video.Status.PrivacyStatus=“未列出”;
//使用代码段,下面的状态抛出401(未授权问题)。
//单独使用代码段抛出404(错误请求)。
//在这两种情况下,参数baseURI都会引发Null异常。
var videosInsertRequest=youtubeService.Videos.Insert(视频,“片段”,上传流,内容类型);
videosInsertRequest.ProgressChanged+=videosInsertRequest\u ProgressChanged;
videosInsertRequest.ResponseReceived+=videosInsertRequest_ResponseReceived;
Google.api.Upload.IUploadProgress progress=wait videosInsertRequest.UploadAsync();
开关(进度状态)
{
案例上传状态。上传:
Write(String.Format(“{0}字节已发送。”,progress.BytesSent));
打破
案例上载状态。失败:
Write(String.Format(“{0}
”,progress.Exception.Message));
Write(String.Format(“{0}
”,progress.Exception.StackTrace));
打破
}
//还尝试从服务器路径读取文件,而不是通过fileupload控件上载。
/*
使用(var fileStream=newfilestream(HttpContext.Current.Server.MapPath(“~/1.mp4”)、FileMode.Open))
{
var videosInsertRequest=youtubeService.Videos.Insert(视频,“片段,状态”,文件流,内容类型);
videosInsertRequest.ProgressChanged+=videosInsertRequest\u ProgressChanged;
videosInsertRequest.ResponseReceived+=videosInsertRequest_ResponseReceived;
等待videosInsertRequest.UploadAsync();
}
*/
}
捕获(例外情况除外)
{
响应.写入(例如Message+“”+ex.StackTrace);
}
最后
{
}
}
void videosInsertRequest_进度已更改(Google.api.Upload.IUploadProgress进度)
{
开关(进度状态)
{
案例上传状态。上传:
Write(String.Format(“{0}字节已发送。”,progress.BytesSent));
打破
案例上载状态。失败:
Response.Write(String.Format(“{0}
”),progress.Exceptio
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet", uploadedStream, contenttype);
var videosInsertRequest = youtubeService.Videos.Insert(video, "snippet,status", uploadedStream, contenttype);