使用GoogleApisClient ServiceAccountCredential调用RequestAccessTokenAsync抛出异常抛出、异常、GoogleApisClient、Servi

2023-09-04 09:43:28 作者:你玩不起爱情

背景:我已成立了一个服务帐户我的谷歌开发者控制台项目,并使用服务帐户的电子邮件,证书和加密口令和下面的例子提供了GoogleAPisSample Plus.ServiceAccount。下面的代码片段是我的Windows服务应用程序的一部分:

Background: I have set up a Service Account for my project on Google Developer Console and using the Service Account Email, the Certificate and the secret password and following the example provide in the GoogleAPisSample Plus.ServiceAccount. The snippet below is part of my Windows Service application:

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和服务进行并执行的分析数据细阅读运行此服务。

When I install and run this service on my local Development machine it does the credential.RequestAccessTokenAsync fine and receives the AccessToken and the service carries on and does the reading of the Analytics data fine.

然而,当它部署在我们的QA环境(窗口Server 2008 R2标准),并再次运行,以下异常被抛出时,credential.RequestAccessTokenAsync叫做:

However when it is deployed on our QA environment (Window Server 2008 R2 Standard) and run again, the following exception gets thrown when credential.RequestAccessTokenAsync is called:

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以下的最新版本:

I have made sure I have the latest versions of the following in 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如下:

and the following in 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环境(窗口Server 2008 R2标准)?

So, My Question is why does it work fine on my Dev Machine and throws that Exception that I mentioned above on our QA Environment (Window Server 2008 R2 Standard)?

推荐答案

找到了解决办法!这是无关的谷歌Analytics API。它是下列两件事情的组合:

Found the solution! It was nothing to do with the Google Analytics API. It was a combination of the following two things:

在安装 KB2468871 .NET 4.0补丁对我们的质量保证和Live服务器。

更​​新,以进一步下列DLL中的app.config新版本: Installing KB2468871 .Net 4.0 Patch on our QA and Live Servers.

Updating to further newer versions of the following dlls in app.config:

&LT; dependentAssembly&GT;  &LT; assemblyIdentity名=System.Runtime公钥=b03f5f7f11d50a3a文化=中性/&GT;                 &LT; bindingRedirect oldVersion =0.0.0.0-2.6.6.0NEWVERSION =2.6.6.0/&GT;               &LT; / dependentAssembly&GT;               &LT; dependentAssembly&GT;                 &LT; assemblyIdentity名=System.Threading.Tasks公钥=b03f5f7f11d50a3a文化=中性/&GT;                 &LT; bindingRedirect oldVersion =0.0.0.0-2.6.6.0NEWVERSION =2.6.6.0/&GT;               &LT; / dependentAssembly&GT;               &LT; dependentAssembly&GT;                 &LT; assemblyIdentity名=System.Net.Http公钥=b03f5f7f11d50a3a文化=中性/&GT;                 &LT; bindingRedirect oldVersion =0.0.0.0-2.2.18.0NEWVERSION =2.2.18.0/&GT;               &LT; / dependentAssembly&GT;               &LT; dependentAssembly&GT;                 &LT; assemblyIdentity名=System.Net.Http.Primitives公钥=b03f5f7f11d50a3a文化=中性/&GT;                 &LT; bindingRedirect oldVersion =0.0.0.0-2.2.18.0NEWVERSION =2.2.18.0/&GT;               &LT; / dependentAssembly&GT;               &LT; dependentAssembly&GT;                 &LT; assemblyIdentity名=Microsoft.Threading.Tasks.Extensions.Desktop公钥=b03f5f7f11d50a3a文化=中性/&GT;                 &LT; bindingRedirect oldVersion =0.0.0.0-1.0.165.0NEWVERSION =1.0.165.0/&GT;               &LT; / dependentAssembly&GT;             &LT; / assemblyBinding&GT;

<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>