gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:41:48 24 4
gpt4 key购买 nike

我们正在使用 Google Analytics API v3 (点网版)用于报告我们网站上的一些统计数据。我的代码在本地机器上运行良好,但由于某些防火墙规则,它无法在生产服务器上运行。我们的系统管理员建议尝试使用代理。我在互联网上搜索了为 Google Analytics 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 配置。

 <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 类库发生变化时,我们就不必费心重新编写代码来设置代理。 :-)

关于.net - 通过代理服务器路由 Google Analytics v3 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16355320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com