.net core 如何修复错误:“引用;Google.api.Auth:至少应设置一个客户端机密(已安装或网络);给YouTubeAPI

.net core 如何修复错误:“引用;Google.api.Auth:至少应设置一个客户端机密(已安装或网络);给YouTubeAPI,.net-core,google-api,youtube-api,azure-functions,azure-logic-apps,.net Core,Google Api,Youtube Api,Azure Functions,Azure Logic Apps,我已经创建了一个HTTP触发器azure函数,它保存了将视频自动上传到YouTube的代码(如下)。资料来源:() 当我尝试使用visual studio在本地运行应用程序时,出现以下错误: 执行“功能1”(失败,Id=d601d64a-2f2c-4f8a-8053-a2f33ca21dbc) System.Private.CoreLib:执行函数Function1时发生异常。 Google.api.Auth:至少有一个客户端机密(已安装或通过Web) 应该设置 这看起来像是谷歌的身份验证错误,

我已经创建了一个HTTP触发器azure函数,它保存了将视频自动上传到YouTube的代码(如下)。资料来源:()

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

执行“功能1”(失败,Id=d601d64a-2f2c-4f8a-8053-a2f33ca21dbc) System.Private.CoreLib:执行函数Function1时发生异常。 Google.api.Auth:至少有一个客户端机密(已安装或通过Web) 应该设置

这看起来像是谷歌的身份验证错误,但我不确定如何修复它,我看到YouTube API不支持服务帐户?如何解决这个问题,有没有解决的办法?提前谢谢

C#代码:

使用系统;
使用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);
}
}
}

看起来您试图使用服务帐户来执行OAuth2 web服务器流,但这不起作用。创建服务帐户凭据的正确代码形式如下所示

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
              {
                    credential = GoogleCredential.FromStream(stream)
                         .CreateScoped(scopes);
                }

正如我在您的其他问题中提到的,YouTube API不支持服务帐户身份验证。您必须使用Oauth2,我不相信这可以在azure函数内部完成。因为无法生成web浏览器窗口来请求用户的授权。

看起来您试图使用服务帐户来执行OAuth2 web服务器流,但这不起作用。创建服务帐户凭据的正确代码形式如下所示

GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
              {
                    credential = GoogleCredential.FromStream(stream)
                         .CreateScoped(scopes);
                }

正如我在您的其他问题中提到的,YouTube API不支持服务帐户身份验证。您必须使用Oauth2,我不相信这可以在azure函数内部完成。由于无法生成web浏览器窗口来请求用户授权。

@DalmTo,我理解您的观点,它不能