1

我正在一个网站上工作,其中一部分是关于网站统计的,我想从 Google Analytic API 获取数据,现在它在我的本地主机上工作正常,但是当我将它上传到我的服务器时,它显示错误:

[CryptographicException: An internal error occurred.
]
   System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +33
   System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) +0
   System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags) +218
   System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags) +65
   System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password, X509KeyStorageFlags keyStorageFlags) +61
   PortalCore.Classes.GAHelper.Credential(String accemail, String certname) +119
   PortalCore.Admin.Stat.SetValues() +629
   PortalCore.Admin.Stat.Page_Load(Object sender, EventArgs e) +5
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

我的代码:

string startdate = installeddate.ToString("yyyy-MM-dd"); //installeddate from My DB it's fine

//Creditional:
var creditional = GAHelper.Credential(GAHelper.GetGaData().CreditionalEmail,GAHelper.GetGaData().CertificationKey);
//Creating AnalyticsService object:
var service = GAHelper.CreateAlanlytcsObject(creditional);

//ProfileID:
string profileid = "XXXXXXXX";

//Getting data:
var newvisits = GAHelper.GetGaResults(service, profileid, startdate, DateTime.Now.ToString("yyyy-MM-dd"),"ga:newVisits");

// Getting results:
if (newvisits != null && newvisits.Rows != null)
                    {
                        ltNewVisits.Text = newvisits.Rows[0][0];
                    }

和我的助手类:GAHelper.cs

public ServiceAccountCredential Credential(string accemail, string certname)
        {
            var certificate = new X509Certificate2(HttpContext.Current.Server.MapPath("~/Certifications/" + certname), "notasecret",X509KeyStorageFlags.Exportable);


                ServiceAccountCredential credential = new ServiceAccountCredential(
                   new ServiceAccountCredential.Initializer(accemail)
                   {
                       Scopes = new[] { AnalyticsService.Scope.Analytics.ToLower() }
                   }.FromCertificate(certificate));
                return credential;

        }

public AnalyticsService CreateAlanlytcsObject(ServiceAccountCredential credential)
        {
                var service = new AnalyticsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "IranPortal",
                });
                return service;
        }

public GaData GetGaResults(AnalyticsService service, string profileid,string startdate,string enddate,string metrics)
        {
                return service.Data.Ga.Get("ga:" + profileid, startdate, enddate, metrics).Execute();

        }

谢谢你们。

4

1 回答 1

7

我找到了解决方案,我应该将我的凭据更改为:

var certificate = 
    new X509Certificate2
    (HttpContext.Current.Server.MapPath("~/Certifications/" + certname), 
    "notasecret", 
    X509KeyStorageFlags.MachineKeySet |
    X509KeyStorageFlags.PersistKeySet | 
    X509KeyStorageFlags.Exportable);


ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(accemail)
    {
        Scopes = new[] { AnalyticsService.Scope.Analytics.ToLower() }
    }.FromCertificate(certificate));

return credential;
于 2013-11-10T21:46:41.803 回答