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