【问题标题】:Calling Google APIs with service account c#使用服务帐户 c# 调用 Google API
【发布时间】:2019-11-06 12:03:04
【问题描述】:

我面临以下问题:我创建了一个 google 服务帐户,现在我正在尝试使用 c# 对 Google API 和 YouTube Data API 进行授权 API 调用。这是我的示例代码:

string filePath = @"~\my-path\Default-GASvcAcct-508d097b0bff.json"

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
    googleCredential = GoogleCredential.FromStream(stream);
}

if (googleCredential.IsCreateScopedRequired)
{
    googleCredential.CreateScoped(
        new string[] 
        {
            YouTubeService.Scope.YoutubeReadonly,
            YouTubeService.Scope.YoutubeUpload,
            YouTubeService.Scope.Youtube,
            YouTubeService.Scope.YoutubeForceSsl,
            YouTubeService.Scope.Youtubepartner,
            YouTubeService.Scope.YoutubepartnerChannelAudit
        });
}

YouTubeService service = new YouTubeService(new BaseClientService.Initializer()
{
    HttpClientInitializer = googleCredential,
});

service.Videos.List()

代码构建成功,但是当我启动程序时,我收到以下错误消息:

System.IO.FileNotFoundException: '无法加载文件或程序集 'BouncyCastle.Crypto,版本=1.7.4137.9688,文化=中性, PublicKeyToken=a4292a325f69b123' 或其依赖项之一。这 系统找不到指定的文件。'

有人知道如何修复此错误,是否可以使用服务帐户凭据向 YouTube 数据 API 发出请求?

提前致谢!

【问题讨论】:

  • 您使用什么版本的 .NET?
  • 您使用的是哪个版本的 NuGet 包?请注意,您也忽略了 CreateScoped 的返回值 - 您应该拥有 googleCredential = googleCredential.CreateScoped(...)
  • 检查这个可能对你有帮助stackoverflow.com/questions/21132531/…
  • @Baruch 在这个项目中我使用的是 .NET 4.5 - 你认为 .NET 的版本可能是导致此错误的原因吗?在 using 语句中已抛出异常。
  • @MangeshAuti 问题不在于实现列表视频功能,而是实现授权。

标签: c# youtube-data-api google-oauth google-api-dotnet-client service-accounts


【解决方案1】:

更新:

根据 Jhon Skeet 的评论,如果您更新 Google API nuget,您将不再依赖 BouncyCastle.Crypto nuget,因此这也是一种选择。

原答案:

我无法明确证明,但似乎 nuget BouncyCastle.Crypto 不适合 .NET 4.0

您可以看到有一个nuget 试图使可移植性。

而且我也认为有一个预发布版本应该与.NET 4.0兼容:

【讨论】:

  • 我们已于 2016 年 12 月停止在 Google API 库中使用 BouncyCastle。最好的解决方案是使用最新版本,而不是试图让 BouncyCastle 正常工作。
猜你喜欢
  • 2019-01-23
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 2014-02-01
  • 2018-10-06
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多