我一直在使用Google.GData.Analytics
和Google.GData.Client
检索一些谷歌分析数据,虽然谷歌分析API v2在我的web应用程序。
代码如下:
string username = "abc@gmail.com";
string password = "myPasswordHere";
string profileId = "ga:88376970";
string _from = Convert.ToDateTime(dtpFrom.SelectedDate.Value.ToString()).ToString("yyyy-MM-dd");
string _to = Convert.ToDateTime(dtpTo.SelectedDate.Value.ToString()).ToString("yyyy-MM-dd");
AccountQuery AccountsQuery = new AccountQuery();
service = new AnalyticsService("DoogalAnalytics");
service.setUserCredentials(username, password);
try
{
DataFeedUrl = "https://www.google.com/analytics/feeds/data";
DataQuery PageViewQuery = new DataQuery(DataFeedUrl)
{
Ids = profileId ,
Metrics = "ga:pageviews",
Dimensions = "ga:date",
Sort = "ga:date",
GAStartDate = _from,
GAEndDate = _to
};
StringBuilder strData = new StringBuilder();
int maxValue = 0;
int today = 0;
List<int> gList = new List<int>();
foreach (DataEntry entry in service.Query(PageViewQuery).Entries)
{
var value = entry.Metrics.First().IntegerValue;
gList.Add(value);
maxValue = value > maxValue ? value : maxValue;
today = entry.Metrics.Last().IntegerValue;
strData.AppendFormat("{0},", value);
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
这段代码工作得很好,直到大约5天前,我一直在使用这段代码在过去的7或8个月,但现在突然我面临的错误
执行认证请求返回意外结果:404。
当我搜索谷歌。我找了很多,但没有找到任何解决办法。如有任何帮助或指导,将不胜感激。
客户端登录于2015年4月20日开始中断/关闭,可能在2015年5月26日左右完成。你不能再使用客户端登录(登录和密码)与谷歌分析API,你需要切换到Oauth2。您需要更改代码以使用Oauth2或服务帐户。
最好使用最新版本的客户端库,它使用Google Analytics V3,你正在使用V2。
Install-Package google . api . analytics .v3
如果这是你已经拥有的数据,你可能需要考虑一个服务帐户。服务帐户将允许您设置访问您的谷歌分析帐户,它不需要用户验证您的代码。如果这不是你的Google Analytics帐户,而是由另一个用户拥有的帐户,那么你将需要使用Oauth2并从用户请求身份验证。
My Tutorial: Google Analytics API Authentication with c#
从上面的教程中摘取的代码,教程是保持最新的,这段代码可能不是:
string[] scopes = new string[] {
AnalyticsService.Scope.Analytics, // view and manage your Google Analytics data
AnalyticsService.Scope.AnalyticsEdit, // Edit and manage Google Analytics Account
AnalyticsService.Scope.AnalyticsManageUsers, // Edit and manage Google Analytics Users
AnalyticsService.Scope.AnalyticsReadonly}; // View Google Analytics Data
String CLIENT_ID = "6.apps.googleusercontent.com"; // found in Developer console
String CLIENT_SECRET = "xxx";// found in Developer console
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential =
GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets {
ClientId = CLIENT_ID
, ClientSecret = CLIENT_SECRET
}
, scopes
, Environment.UserName
, CancellationToken.None
, new FileDataStore("Daimto.GoogleAnalytics.Auth.Store")).Result;
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",
});
DataResource.GaResource.GetRequest request = service.Data.Ga.Get("ga:8903098", "2014-01-01", "2014-01-01", "ga:sessions");
request.MaxResults = 1000;
GaData result = request.Execute();