我花了一些时间在C#中编写控制台应用程序,并与Google AnalyticsAPI进行通信。
当我将代码作为独立的控制台应用程序执行时,一切正常。但是,只要我尝试将代码集成到SharePoint(或其他IIS托管的软件)中作为ASP.NET类型的解决方案,它就无法正常工作,并且在加载证书时会不断失败。 / p>
以下是我使用的代码:
var serviceAccountEmail = "xxxx@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"CertFile-privatekey.p12", "password", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { AnalyticsService.Scope.Analytics }
}.FromCertificate(certificate));
var gas = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GATest",
});
var r = gas.Data.Ga.Get("ga:xxxxxx", "2014-01-01", "2014-02-01", "ga:pageviews");
//Specify some addition query parameters
r.Dimensions = "ga:source";
r.Filters = "ga:pagePath=~/business/,ga:medium==referral";
r.Segment = "gaid::-1";
r.Sort = "-ga:pageviews";
r.MaxResults = 50;
//Execute and fetch the results of our query
Google.Apis.Analytics.v3.Data.GaData d = r.Execute();
其余的逻辑并不是一个至关重要的部分,因为它只处理渲染。
集成(IIS托管)应用程序中断的行是这一行:
var certificate = new X509Certificate2(@"CertFile-privatekey.p12", "password", X509KeyStorageFlags.Exportable);
我想这些问题总结如下:
对于实际的错误消息,Visual Studio只是崩溃并且说抛出了异常。这发生在X509Certificate2-line上。
谢谢: - )