通过代理服务器路由Google Analytics v3 API



我们使用Google Analytics API v3 (dot net版本)来报告我们网站上的一些统计数据。我的代码在本地机器上运行良好,但由于某些防火墙规则,它无法在生产服务器上运行。我们的系统管理员建议尝试使用代理。我在互联网上搜索了任何指导方针,以建立代理谷歌分析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;
    }

我没有在论坛或互联网上找到任何有用的文档,所以决定使用这个系统。web.config.

 <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类库发生变化,我们都不必为重新编写代码来设置代理而烦恼。: -)

最新更新