2015-06-01 38 views
3

我正在使用來自GitHub的示例code{「解析值時遇到意外字符:<.Path'',第0行,位置0.」}在google analytics api中

  #region code 

      AnalyticsService service; 

         // Service account Authentication 
      String SERVICE_ACCOUNT_EMAIL =" ";// removed the account email 
      string SERVICE_ACCOUNT_KEYFILE = @"C:\Projects\Test\TMBWebsite-7f016e7c3562.p12"; 
      service = DaimtoAnalyticsAuthenticationHelper.AuthenticateServiceAccount(SERVICE_ACCOUNT_EMAIL,SERVICE_ACCOUNT_KEYFILE); 




      //////Get account summary and display them. 
      foreach (AccountSummary account in DaimtoAnaltyicsManagmentHelper.AccountSummaryList(service).Items) 
      { 
       // Account 
       Console.WriteLine("Account: " + account.Name + "(" + account.Id + ")"); 

       foreach (WebPropertySummary wp in account.WebProperties) 
       { 

        // Web Properties within that account 
        Console.WriteLine("\tWeb Property: " + wp.Name + "(" + wp.Id + ")"); 


        //Don't forget to check its not null. Believe it or not it could be. 
        if (wp.Profiles != null) 
        { 

         foreach (ProfileSummary profile in wp.Profiles) 
         { 
          // Profiles with in that web property. 
          Console.WriteLine("\t\tProfile: " + profile.Name + "(" + profile.Id + ")"); 
         } 
        } 
       } 
      } 

      #endregion 

// Authenicate服務

 if (!File.Exists(keyFilePath)) 
     { 
      Console.WriteLine("An Error occurred - Key file does not exist"); 
      return null;  
     } 

     string[] scopes = new string[] { AnalyticsService.Scope.Analytics, // view and manage your analytics data 
             AnalyticsService.Scope.AnalyticsEdit, // edit management actives 
             AnalyticsService.Scope.AnalyticsManageUsers, // manage users 
             AnalyticsService.Scope.AnalyticsReadonly};  // View analytics data    

     X509Certificate2 certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable); 
     // X509Certificate2 certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); 
     try 
     { 
      ServiceAccountCredential credential = new ServiceAccountCredential(
       new ServiceAccountCredential.Initializer(serviceAccountEmail) 
       { 
        Scopes = scopes 
       }.FromCertificate(certificate)); 

      // Create the service. 
      AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer() 
      { 
       HttpClientInitializer = credential, 
       ApplicationName = "Analytics API Sample", 
      }); 
      return service; 
     } 
     catch (Exception ex) 
     { 

      Console.WriteLine(ex.InnerException); 
      return null; 

     } 

#地區帳戶摘要

/// <summary> 
    /// Lists account summaries (lightweight tree comprised of accounts/properties/profiles) to which the user has access. 
    /// Documentation: https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountSummaries/list 
    /// </summary> 
    /// <param name="service">Valid authenticated Analytics Service</param> 
    /// <returns>List of Account Summaries resource - https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference/management/accountSummaries</returns> 
    public static AccountSummaries AccountSummaryList(AnalyticsService service) 
    { 

     //List all of the activities in the specified collection for the current user. 
     // Documentation: https://developers.google.com/+/api/latest/activities/list 

     //ManagementResource.AccountSummariesResource.ListRequest list = service.Management.AccountSummaries.List(); 
     ManagementResource.AccountSummariesResource.ListRequest list = service.Management.AccountSummaries.List(); 
     list.Alt = ManagementResource.AccountSummariesResource.ListRequest.AltEnum.Json; 
     list.MaxResults = 1000; // Maximum number of Account Summaries to return per request. 

     AccountSummaries feed = list.Execute(); 
     List<AccountSummary> allRows = new List<AccountSummary>(); 

     //var getRequest = settingsService.Groups.Get("[email protected]"); 
     //getRequest.Alt = "json"; 
     //var settings = getRequest.Execute(); 

     //// Loop through until we arrive at an empty page 
     while (feed.Items != null) 
     { 

      allRows.AddRange(feed.Items); 

      // We will know we are on the last page when the next page token is 
      // null. 
      // If this is the case, break. 
      if (feed.NextLink == null) 
      { 
       break; 
      } 

      // Prepare the next page of results    
      list.StartIndex = feed.StartIndex + list.MaxResults; 
      // Execute and process the next page request 
      feed = list.Execute(); 

     } 

     feed.Items = allRows; 

     return feed; 

    } 
    #endregion 

AccountSummaries喂= list.Execute(); 引發異常:{「分析值時遇到意外字符:<。路徑',第0行,位置0.」}在google analytics api中

+0

我想通了。防火牆阻止了我的請求。在我的配置文件中添加了下面的代碼並工作。 <代理proxyaddress = 「」 usesystemdefault = 「真」/>

+0

隨意添加它作爲一個答案,而不僅僅是作爲一個評論。 – peleyal

回答

2

我想通了。防火牆擋住了我的request.Added如下代碼在我的配置文件和工作 <system.net> <defaultProxy useDefaultCredentials="true"> <proxy proxyaddress="My proxy address here" usesystemdefault="True"/> </defaultProxy> </system.net>

+0

對不起,打開一箇舊的線程......我遇到了類似的情況,你的解決方案幫助我。公司防火牆阻止了我的連接。但是,我不想將代理地址硬編碼到應用程序中。有沒有其他的方式可以在運行時將它配置到API中? –

相關問題