使用GoogleApisClient ServiceAccountCredential调用RequestAccessTokenAsync会抛出异常

时间:2014-01-31 15:24:16

标签: .net google-analytics-api system.net.httpwebrequest

背景:我已在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,并且该服务会继续运行,并且可以正确读取Google Analytics数据。

但是当它部署在我们的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标准)上面提到的异常?

1 个答案:

答案 0 :(得分:5)

找到解决方案!这与Google AnalyticsAPM无关。这是以下两件事的组合:

  1. 安装 KB2468871 我们的QA和Live服务器上的.Net 4.0补丁。
  2. 在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>