Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法在C中使用google analytics API上载CSV文件_C#_Google Analytics Api - Fatal编程技术网

C# 无法在C中使用google analytics API上载CSV文件

C# 无法在C中使用google analytics API上载CSV文件,c#,google-analytics-api,C#,Google Analytics Api,我试图上传一个CSV文件使用谷歌分析api,它给了我一个失败的状态。 我得到的错误是变量为null:参数名base uri 我已经在开发者控制台中为google analytics api授予了权限,我正在使用服务帐户生成密钥和id 这是我的代码片段 const string serviceAccountEmail = "716698526626-6s61a3tbe1m5mofo9@developer.gserviceaccount.com"; const str

我试图上传一个CSV文件使用谷歌分析api,它给了我一个失败的状态。 我得到的错误是变量为null:参数名base 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();
        }

我解决了同样的问题。在我的例子中,这是一个错误的数据源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 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上的列表方法检查配置