Using GoogleApisClient ServiceAccountCredential calling RequestAccessTokenAsync throws Exception

与世无争的帅哥 提交于 2019-12-23 09:38:25

问题


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

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.

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

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

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>

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


回答1:


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

  1. Installing KB2468871 .Net 4.0 Patch on our QA and Live Servers.
  2. Updating to further newer versions of the following dlls in app.config:

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



来源:https://stackoverflow.com/questions/21483599/using-googleapisclient-serviceaccountcredential-calling-requestaccesstokenasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!