gpt4 book ai didi

.net - 使用 GoogleApisClient ServiceAccountCredential 调用 RequestAccessTokenAsync 抛出异常

转载 作者:行者123 更新时间:2023-12-04 15:11:36 30 4
gpt4 key购买 nike

背景:我已经在 Google Developer Console 上为我的项目设置了一个服务帐户,并使用服务帐户电子邮件、证书和密码,并遵循 GoogleAPisSample Plus.ServiceAccount 中提供的示例。下面的代码片段是我的 Windows 服务应用程序的一部分:

var List<string> Scopes = new List<string> { "https://www.googleapis.com/auth/analytics.readonly" };

var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = Scopes
}.FromCertificate(certificate));


if(credential.RequestAccessTokenAsync(CancellationToken.None).Result)
{

AuthenticationKey = credential.Token.AccessToken;
}

当我在本地开发机器上安装并运行此服务时,它会正常执行 credential.RequestAccessTokenAsync 并接收 AccessToken,该服务会继续执行并读取分析数据。

但是,当它部署在我们的 QA 环境(Window Server 2008 R2 Standard)上并再次运行时,调用 credential.RequestAccessTokenAsync 时会抛出以下异常:
System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: Method not found: 'System.Net.HttpStatusCode System.Net.Http.HttpResponseMessage.get_StatusCode()'.
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
at OurApplication.SchedulerService.GoogleAnalytics.OAuth2.ServiceAccountCredential.<RequestAccessTokenAsync>d__b.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.get_Result()
at OurApplication.SchedulerService.GoogleAnalytics.GADataFetcher.AuthenticateAndAuthorize()
at OurApplication.SchedulerService.GoogleAnalytics.GADataFetcher..ctor()
at OurApplication.SchedulerService.GoogleAnalytics.GoogleAnalyticsService.GoogleAnalyticsTopPerformances(Int32 sessID, String sessToken)
---> (Inner Exception #0) System.MissingMethodException: Method not found: 'System.Net.HttpStatusCode System.Net.Http.HttpResponseMessage.get_StatusCode()'.
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
at Seatwave.SchedulerService.GoogleAnalytics.OAuth2.ServiceAccountCredential.<RequestAccessTokenAsync>d__b.MoveNext()<---

我已确保在packages.config 中有以下最新版本:
  <package id="Microsoft.Bcl" version="1.1.6" targetFramework="net40" />
<package id="Microsoft.Bcl.Async" version="1.0.165" targetFramework="net40" />
<package id="Microsoft.Bcl.Build" version="1.0.13" targetFramework="net40" />
<package id="Microsoft.Net.Http" version="2.2.18" targetFramework="net40" />

以及 app.config 中的以下内容:
  <dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.1.10.0" newVersion="2.1.10.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.165.0" newVersion="1.0.165.0" />
</dependentAssembly>

所以,我的问题是为什么它在我的开发机器上运行良好,并在我们的 QA 环境(Window Server 2008 R2 标准)上抛出我上面提到的异常?

最佳答案

找到了解决办法!它与 Google Analytics API 无关。它是以下两件事的结合:

  • 安装
    KB2468871
    我们的 QA 和 Live Server 上的 .Net 4.0 补丁。
  • 在 app.config 中更新到以下 dll 的更新版本:
    <dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.6.6.0" newVersion="2.6.6.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.6.6.0" newVersion="2.6.6.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.18.0" newVersion="2.2.18.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.2.18.0" newVersion="2.2.18.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.0.165.0" newVersion="1.0.165.0" />
    </dependentAssembly>
    </assemblyBinding>
  • 关于.net - 使用 GoogleApisClient ServiceAccountCredential 调用 RequestAccessTokenAsync 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21483599/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com