6

我們正在使用Google Analytics API v3(點網版)報告我們網站上的一些統計數據。我的代碼在本地機器上運行良好,但由於某些防火牆規則,它不適用於生產服務器。我們的系統管理員建議嘗試使用代理。我在互聯網上搜索了關於爲Google AnalyticsAPI服務設置代理的任何指導,但沒有運氣。欣賞這方面的任何指針。通過代理服務器路由谷歌分析v3 API

編輯:

public DataTable GetSearchTrends() 
    { 
     string GoogleAnalyticsProfileId = AppConfigManager.GetGoogleAnalyticsProfileIdForInis(); 

     var service = new AnalyticsService(new BaseClientService.Initializer() 
     { 
      Authenticator = Authenticate() 

     }); 

      DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
      GoogleAnalyticsProfileId, 
      string.Format("{0:yyyy-MM-dd}", StartDate), 
      string.Format("{0:yyyy-MM-dd}", EndDate), 
      GoogleAnalyticsSearchUniquesMetric 
      ); 

     request.Dimensions = GoogleAnalyticsSearchKeywordMetric; 
     request.Sort = string.Concat("-", GoogleAnalyticsSearchUniquesMetric); 
     request.MaxResults = NumberOfSearchTrendsToFetch; 

     GaData response = request.Fetch(); 

     return SearchTrendsHelper.ConvertToDataTable(
      response.Rows, 
      SearchTrendsKeywordsExcludeList, 
      NumberOfSearchTrendsToDisplay 
      ); 
    } 


    private IAuthenticator Authenticate() 
    { 
     string GoogleAnalyticsServiceScope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue(); 
     string GoogleApiServiceAccountId = AppConfigManager.GetGoogleApiServiceAccountId(); 
     string GoogleApiServiceAccountKeyFile = AppConfigManager.GetGoogleApiServiceAccountKeyFile(); 
     string GoogleApiServiceAccountKeyPassword = AppConfigManager.GetGoogleApiServiceAccountKeyPassword(); 
     AuthorizationServerDescription desc = GoogleAuthenticationServer.Description; 

     X509Certificate2 key = new X509Certificate2(
      HttpContextFactory.Current.Server.MapPath(GoogleApiServiceAccountKeyFile), 
      GoogleApiServiceAccountKeyPassword, 
      X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet 
      ); 

     AssertionFlowClient client = new AssertionFlowClient(desc, key) { 
      ServiceAccountId = GoogleApiServiceAccountId, 
      Scope = GoogleAnalyticsServiceScope, 
     }; 

     OAuth2Authenticator<AssertionFlowClient> auth = new OAuth2Authenticator<AssertionFlowClient>(
      client, 
      AssertionFlowClient.GetState 
      ); 

     return auth; 
    } 
+0

你可以上傳你的代碼在什麼地方。如何驗證和報告數據。將盡力幫助 – 2013-05-07 17:02:04

+0

@KamranShahid:用源代碼更新了問題 – itsbalur 2013-05-08 16:15:39

回答

3

我沒有找到在論壇或互聯網上的任何有用的文檔,因此決定使用在web.config中的System.Net配置。

<system.net> 
    <defaultProxy useDefaultCredentials="true"> 
     <proxy proxyaddress="http://abc.com:3128" usesystemdefault="True" bypassonlocal="True"/> 
     <bypasslist> 
     <add address="http://xyz.com" /> 
     <add address="http://www.example.com" /> 
     </bypasslist> 
    </defaultProxy> 
    </system.net> 

任何我們不想通過代理的請求,可以添加到<bypasslist>。它具有的附加優勢是,無論何時Google API類庫更改,我們都不必爲重新編寫代碼來設置代理而煩惱。 :-)

+0

你可以給我你添加的項目列表bypasslist 2013-05-25 18:17:34