如何在 ASP.NET 核心 MVC Web 应用程序中使用 UserCredential?

How to use UserCredential in ASP.NET Core MVC web application?

我在创建用户凭据 (oauth 2.0) 以在我的 .NET Core 应用程序中实施 youtube-data api(视频上传)时遇到了一个小问题。

在控制台应用程序中,我从 google 下载凭证文件并像这样使用它:

UserCredential credential;

using (var stream = new FileStream(@"C:\Users\Gaby\source\repos\Youtube Api\client.txt", FileMode.Open, FileAccess.Read))
{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { YouTubeService.Scope.YoutubeUpload },
                "user", CancellationToken.None);
}

然而,我试图在 ASP.NET 核心 MVC 应用程序中使用相同的代码,但出现此错误:

Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]

为了实现这个凭证,我使用了这个代码示例:

 var youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
        });

只需在 Asp.Net 核心应用程序中使用相同的代码即可!