上传CSV Google Analytics数据文件c#

时间:2014-03-10 20:49:43

标签: c# csv google-analytics-api

我正在尝试使用C#将CSV文件上传到Google Analytics。他们在谷歌开发者网站上的一个例子:

https://developers.google.com/analytics/solutions/articles/cost-data-import#custom_data_source

但是,它使用python。我试图用C#重写它,但它并没有像我想象的那样简单。任何帮助?

不知道如何从站点的代码示例中实现媒体,因此“csvToUpload”实际上并没有做任何事情。 (csvToUpload是csv文件目录)。

代码段:

public void UploadCSVFile(string csvToUpload, string uploadDate, string accountID, string webPropertyID, string dataSourceID)
    {   

        FileStream stream2 = new FileStream(csvToUpload, FileMode.Open);

        ManagementResource.DailyUploadsResource.UploadMediaUpload dailyUploads = service.Management.DailyUploads.Upload(accountID, webPropertyID,
            dataSourceID, uploadDate, 1, ManagementResource.DailyUploadsResource.UploadMediaUpload.TypeEnum.Cost, stream2, "Data Test");

        dailyUploads.Reset = true;
        //dailyUploads.UploadAsync();
        dailyUploads.Upload();
        stream2.Close();
    }

想要更新代码和帖子以查看是否有人可以帮助我,这里是此任务的更新代码。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

enter image description here

上传有第二个允许您发送流的上传。将您的csv文件作为流加载并尝试以这种方式发送。

答案 1 :(得分:1)

我知道现在为时已晚,但对于其他人来说,这段代码运行良好:

// Read file to stream
FileStream dataStream = new FileStream(AppWindow.WindowBag["File"].ToString(), FileMode.Open);
// Upload selected file
ManagementResource.DailyUploadsResource.UploadMediaUpload request = AppWindow.Service.Management.DailyUploads.Upload(AppWindow.WindowBag["Account"].ToString(),
                                            AppWindow.WindowBag["Property"].ToString(),
                                            AppWindow.WindowBag["Dataset"].ToString(),
                                            "2014-04-14",
                                            1,
                                            ManagementResource.DailyUploadsResource.UploadMediaUpload.TypeEnum.Cost,
                                            dataStream, "application/octet-stream");
request.Reset = true;
request.ProgressChanged += request_ProgressChanged;
request.ResponseReceived += request_ResponseReceived;
request.Upload();
dataStream.Close();