【发布时间】:2013-05-03 09:11:34
【问题描述】:
我们使用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;
}
【问题讨论】:
-
你能把你的代码上传到某个地方吗?如何验证和报告数据。届时将尽力提供帮助
-
@KamranShahid:用源代码更新了问题
标签: .net google-api google-analytics-api google-api-dotnet-client