繁体   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