簡體   English   中英

無法在C#中使用Google Analytics API上傳CSV文件

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

我正在嘗試使用Google Analytics(分析)API上傳CSV文件,但狀態為我失敗。 我得到的錯誤是變量為null:參數名稱基於uri

我已經在開發人員控制台中授予了Google Analytics(分析)API的權限,並且正在使用服務帳戶來生成密鑰和ID。

這是我的代碼片段

        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();
        }

我解決了同樣的問題。 就我而言,這是錯誤的DataSourceId。 嘗試這個:

 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();
    }

在service.Management.CustomDataSources和service.Management.Uploads上按此順序使用List方法,以檢查您的配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM