2014-09-22 61 views
1

我想上传一个CSV文件使用谷歌分析API,它给了我一个失败的状态。 我得到的错误是可变的为null:参数名称基地uri无法上传CSV文件使用谷歌分析API在C#

我已经在开发人员控制台中授予了Google Analytics(分析)API的权限,并且我使用服务帐户来生成密钥和ID。

这是我的代码片断

 const string serviceAccountEmail = "[email protected]"; 
     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(); 
     } 

+0

那么,它是空的? – Casey 2014-09-24 04:05:36

回答

0

我解决了同样的问题。在我的情况下,它是一个错误的DataSourceId。在此为了检查你的配置

const string serviceAccountEmail = "[email protected]"; 
    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使用列表方法: 试试这个。