.net core 创建使用YouTube API将视频上载到YouTube的函数时出错?

.net core 创建使用YouTube API将视频上载到YouTube的函数时出错?,.net-core,youtube-api,azure-functions,azure-logic-apps,azure-blob-storage,.net Core,Youtube Api,Azure Functions,Azure Logic Apps,Azure Blob Storage,我创建了这个azure函数,其中包含自动将视频上传到我的YouTube频道()的代码。我把它作为HTTP触发器函数,当我的Azure Blob容器中上传了新视频时,该函数应该会被触发 当我尝试使用Visual Studio在本地运行应用程序时,出现以下错误: 执行“功能1”(失败,Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f) System.Private.CoreLib:执行函数Function1时发生异常。 System.Private.CoreLib:找不

我创建了这个azure函数,其中包含自动将视频上传到我的YouTube频道()的代码。我把它作为HTTP触发器函数,当我的Azure Blob容器中上传了新视频时,该函数应该会被触发

当我尝试使用Visual Studio在本地运行应用程序时,出现以下错误:

执行“功能1”(失败,Id=84400f0c-b6e4-4c78-bf55-30c4527a8b5f) System.Private.CoreLib:执行函数Function1时发生异常。 System.Private.CoreLib:找不到文件 'C:\Users\Peter\Desktop\TestDemo\UploadVideo\UploadVideo\bin\Debug\netcoreapp2.1\client\u secrets.json'

我不知道如何修复这个错误,以及为什么会出现500个错误。这个错误意味着什么?我是否需要进入并在我的目录中手动创建client_secrets.json文件,还是在运行函数时创建的

代码:

使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用Google.api.Auth.OAuth2;
使用Google.api.Upload;
使用Google.api.YouTube.v3.Data;
运用系统反思;
使用Google.api.YouTube.v3;
使用Google.api.Services;
使用系统线程;
命名空间上载视频
{
公共静态类函数1
{
[功能名称(“功能1”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
登录信息(“YouTube数据API:上传视频”);
日志。登录信息(“====================================================”);
尝试
{
等待运行();
}
捕获(聚合异常)
{
foreach(ex.InnerExceptions中的变量e)
{
登录信息(“错误:+e.Message”);
}
}
返回新的OkObjectResult($“视频处理…”);
}
专用静态异步任务运行()
{
用户凭证;
使用(var stream=newfilestream(“client_secrets.json”、FileMode.Open、FileAccess.Read))
{
凭证=等待GoogleWebAuthorizationBroker.AuthorizationAsync(
GoogleClientSecrets.Load(stream.Secrets),
//此OAuth 2.0访问范围允许应用程序将文件上载到
//已验证用户的YouTube频道,但不允许其他类型的访问。
新[]{YouTubeService.Scope.YoutubeUpload},
“用户”,
取消令牌。无
);
}
var youtubeService=new youtubeService(new BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=Assembly.GetExecutionGassembly().GetName().Name
});
var video=新视频();
video.Snippet=新的VideoSnippet();
video.Snippet.Title=“默认视频标题”;
video.Snippet.Description=“默认视频描述”;
video.Snippet.Tags=新字符串[]{“tag1”、“tag2”};
video.Snippet.CategoryId=“22”//请参阅https://developers.google.com/youtube/v3/docs/videoCategories/list
video.Status=新的VideoStatus();
video.Status.PrivacyStatus=“未列出”;//或“私有”或“公共”
var filePath=@“C:\Users\Peter\Desktop\audio\test.mp4”//替换为实际电影文件的路径。
使用(var fileStream=newfilestream(filePath,FileMode.Open))
{
var videosInsertRequest=youtubeService.Videos.Insert(视频,“片段,状态”,文件流,“视频/*”;
videosInsertRequest.ProgressChanged+=videosInsertRequest\u ProgressChanged;
videosInsertRequest.ResponseReceived+=videosInsertRequest_ResponseReceived;
等待videosInsertRequest.UploadAsync();
}
}
私有静态void videosInsertRequest_ProgressChanged(Google.api.Upload.IUploadProgress)
{
开关(进度状态)
{
案例上传状态。上传:
WriteLine(“{0}字节已发送。”,progress.BytesSent);
打破
案例上载状态。失败:
WriteLine(“一个错误阻止上载完成。\n{0}”,progress.Exception);
打破
}
}
私有静态无效视频接收请求_响应(视频)
{
WriteLine(“视频id{0}已成功上载。”,Video.id);
}
}
}

根据错误,该目录中不存在client_secrets.json。你能检查一下吗?是的,它不存在。我需要手动创建那个json文件吗?你可以尝试这样做吗?@JimXu我手动创建了这个json文件,但我现在发现了这个错误:“执行了'Function1'(失败,Id=f0094630-0e4d-4e5d-ae27-4b813d027655)System.Private.CoreLib:执行function:Function1.Google.api.Auth时异常:至少一个客户端机密(已安装或Web)应设置“是否知道如何解决此问题?有关此问题,请参阅
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Upload;
using Google.Apis.YouTube.v3.Data;
using System.Reflection;
using Google.Apis.YouTube.v3;
using Google.Apis.Services;
using System.Threading;

namespace UploadVideo
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            log.LogInformation("YouTube Data API: Upload Video");
            log.LogInformation("==============================");

            try
            {
                await Run();
            }
            catch (AggregateException ex)
            {
                foreach (var e in ex.InnerExceptions)
                {
                    log.LogInformation("Error: " + e.Message);
                }
            }

            return new OkObjectResult($"Video Processed..");

        }

        private static 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 = @"C:\Users\Peter\Desktop\audio\test.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();
            }
        }

        private static 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;
            }
        }

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