google-api-dotnet-client: Google Analytics API - Error occurred while sending the request - Service account issue

Hi , I am using service account to get the data from Google analytics. And its working fine in local. But not working after deployment in IIS. Getting error an error occurred while sending the request.

1

image

Please find my code below:

try
{

    var service = new AnalyticsService();
    string[] scopes = new string[] { AnalyticsService.Scope.Analytics };              // view and manage your Google Analytics data
    var keyFilePath = Server.MapPath(Url.Content("~/Content/CertificateKey/key.p12"));
    var serviceAccountEmail = "******@******.iam.gserviceaccount.com";

    //loading the Key file
    var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet
        | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

    var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
    {
        Scopes = scopes
    }.FromCertificate(certificate));


    service = new AnalyticsService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "my Website",

    });


    DataResource.GaResource.GetRequest request = service.Data.Ga.Get("ga:18261****", fromDate, toDate, "ga:users");
    request.MaxResults = 1000;
    GaData result = request.Execute();
    ViewBag.GAUsers = result.Rows[0].First().ToString();

}

catch (Exception ex)
{
    ViewBag.errorMsg = ex.Message.ToString();
}

and in errorMsg i got the message “An error occurred while sending the request”.

Someone kindly look into this issue and provide me the solution please. Thank you .

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 44

Most upvoted comments

I checked with IT team .Now They have enabled Port. Its working now. Thanks for your effort. Thank you so much.