在Azure网站中授权Google Analytics请求

时间:2015-04-03 23:36:31

标签: azure asp.net-web-api google-analytics google-analytics-api

我正在尝试通过Azure网站中托管的WEB API控制器调用Google AnalyticsAPI。

我的代码是:

            var certificate = new X509Certificate2(path, "notasecret", X509KeyStorageFlags.Exportable);

            var credentials = new ServiceAccountCredential(
               new ServiceAccountCredential.Initializer("XXXXXXXXXXXXXXXXXXXXXX@developer.gserviceaccount.com")
               {
                   Scopes = new[] { AnalyticsService.Scope.AnalyticsReadonly }
               }.FromCertificate(certificate));

            Service = new AnalyticsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
                ApplicationName = "yoLearn"
            });

我认为路径设置正确:

使用Server.Mappath( “〜/内容/ yoLearn-995f8379d7e4.p12”)

问题是,如果我将我的标志从Exportable更改为MachineKeySet,则会引发此异常:

enter image description here

但如果我让Exportable标志出现在网站上:

enter image description here

如果有人可以在这里帮助我...我只是试图从azure网站查询谷歌分析而生气!

此致

1 个答案:

答案 0 :(得分:0)

这救了我......我必须在证书的构造函数中添加一些标志。

var certificate = new X509Certificate2(path," notasecret",X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

这使得技巧和身份验证运行完全正常