简体   繁体   中英

Unable to upload CSV file using google analytics API in C#

I'm trying to upload a CSV file using google analytics api and it gives me a status failed. The error that I'm getting is variable is null: parameter name base uri

I have given the permissions for google analytics api in developer console and I'm using service account to generate the key and the id's.

This is my code snippet

        const string serviceAccountEmail = "716698526626-6s61a3tbe1m5mofo9@developer.gserviceaccount.com";
        const string serviceAccountPKCSP12FilePath = @"C:\SVN\GAPforInboundandWeb-1f6bfb.p12";
        X509Certificate2 certificate = new X509Certificate2(serviceAccountPKCSP12FilePath, "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                Scopes = new[] { AnalyticsService.Scope.Analytics }
            }.FromCertificate(certificate));
        var service = new AnalyticsService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
            ApplicationName = "GAPforInboundandWeb"
        });

        FileStream realCsv = new FileStream("c:\\real.csv", FileMode.Open);
        // Create the service.


        try
        {
            var upload = service.Management.DailyUploads.Upload("50288725", "UA-50288725-1", "Oy_0JTPvRGCB3Vg5OKVIMQ",
                "2014-09-22", 1, ManagementResource.DailyUploadsResource.UploadMediaUpload.TypeEnum.Cost, realCsv,
                "application/octet-stream");
            upload.Reset = true;
            var res = upload.Upload();
            res.BytesSent.ToString();
            res.Status.ToString();
        }

I resolved the same problem. In my case, its a wrong DataSourceId. Try this:

 const string serviceAccountEmail = "716698526626-6s61a3tbe1m5mofo9@developer.gserviceaccount.com";
    const string serviceAccountPKCSP12FilePath = @"C:\SVN\GAPforInboundandWeb-1f6bfb.p12";
    X509Certificate2 certificate = new X509Certificate2(serviceAccountPKCSP12FilePath, "notasecret", X509KeyStorageFlags.Exportable);

    ServiceAccountCredential credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            Scopes = new[] { AnalyticsService.Scope.Analytics }
        }.FromCertificate(certificate));
    var service = new AnalyticsService(new BaseClientService.Initializer
    {
        HttpClientInitializer = credential,
        ApplicationName = "GAPforInboundandWeb"
    });

    FileStream realCsv = new FileStream("c:\\real.csv", FileMode.Open);
    // Create the service.


    try
    {
        var dataSource = srv.Management.CustomDataSources.List(AccountId, WebId).Execute();
        var strDataSourceId = "Oy_0JTPvRGCB3Vg5OKVIMQ";
        if(dataSource!=null && dataSource.Items.Length>0)
          strDataSourceId =dataSource.Items[0].Id;
        var upload = service.Management.DailyUploads.Upload("50288725", "UA-50288725-1", strDataSourceId,
            "2014-09-22", 1, ManagementResource.DailyUploadsResource.UploadMediaUpload.TypeEnum.Cost, realCsv,
            "application/octet-stream");
        upload.Reset = true;
        var res = upload.Upload();
        res.BytesSent.ToString();
        res.Status.ToString();
    }

Use List methods on service.Management.CustomDataSources and service.Management.Uploads in this order for check your config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM