C# 中的 Google Analytics API - 请求执行失败:https://www.google.com/analytics/feeds/accounts/default [英] Google Analytics API in C# -Execution of request failed: https://www.google.com/analytics/feeds/accounts/default

查看:29
本文介绍了C# 中的 Google Analytics API - 请求执行失败:https://www.google.com/analytics/feeds/accounts/default的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问 Google 分析数据,并且我从 Google 数据 API SDK 获得了样本.但是这些编码不起作用并抛出异常

i want to access Google analytic data and i got samples from Google data API SDK. but these coding does not working and throws exception

请求执行失败:https://www.google.com/analytics/供稿/帐户/默认

所以我发现原因是谷歌将其更新为 v3.0.我搜索了 C# 的更新编码,但找不到解决方案.

so i found the reason for this is Google updated it's to v3.0. i searched updated coding for the C#, but i couldn't find solution for this.

我有同样的问题,但使用 C#.使用 GData .NET Analytics API 时抛出的异常

i have same problem as this, but with C#. Exception thrown when using GData .NET Analytics API

我尝试按照 Google 开发人员中的说明进行编码并进行如下更改 - https://developers.google.com/analytics/resources/articles/gdata-migration-guide#appendix_a

i tried coding with doing changes as follows as it says in Google developer - https://developers.google.com/analytics/resources/articles/gdata-migration-guide#appendix_a

string userName = this.Username.Text;
string passWord = this.Password.Text;

AnalyticsService service = new AnalyticsService("AnalyticsSampleApp");
service.setUserCredentials(userName, passWord);
string googleAccountWebId = "AIXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string profileFeedUrl = "https://www.googleapis.com/analytics/v2.4/data?key=" + googleAccountWebId;

DataQuery query2 = new DataQuery(profileFeedUrl);
query2.Ids = "12345678";
query2.Metrics = "ga:visits";
query2.Sort = "ga:visits";
query2.GAStartDate = DateTime.Now.AddMonths(-1).AddDays(-2).ToString("2011-08-01");
query2.GAEndDate = DateTime.Now.ToString("2013-09-01");
query2.StartIndex = 1;


DataFeed data = service.Query(query2);


foreach (DataEntry entry in data.Entries)
{
    string st=entry.Metrics[0].Value;       
}

但即使我改变了它也会在

but even i change this it throws exception in

DataFeed 数据 = service.Query(query2);

DataFeed data = service.Query(query2);

这一行.例外如下:

请求执行失败:https://www.googleapis.com/analytics/v2.4/data?key=AIXXXXXXXXXXXXXXXXXXXXXX-8&start-index=1&end-date=2013-09-01&ids=12345678&metrics=ga:visits&sort=ga:visits&start-date=2011-08-01

我正在使用以下 DLL

i'm using following DLL

Google.GData.Analytics.dll
Google.GData.Client.dll
Google.GData.Extensions.dll

我的问题:

  1. 我该如何纠正这个错误?

  1. how can i correct this error?

我如何访问 Google 分析数据?这个对吗?否则有什么方法可以做到?例如,我想获取可用的 ProfileId 及其值.(标题和页面浏览量)

how can i access Google analytic data? is this correct? or else what is the way to doing it?? for a example i want to get available ProfileId and their values. (Title and Page views)

推荐答案

Analytics 帐户:

我假设您已经有一个分析帐户,如果您还没有创建一个,然后在此处注册您的域:http://www.google.com/intl/en/analytics/

I am assuming you have an analytics account already if you don't then create one, and sign up your domain here: http://www.google.com/intl/en/analytics/

要获取您的 API 密钥,请执行以下操作:

按照 https://developers.google.com 上的说明进行操作/analytics/resources/articles/gdata-migration-guide(在 Google API 控制台中创建一个项目)以生成您的密钥一旦您将其设置为查询字符串的一部分以请求 Google Analytics 服务,在此案件:您的APIkEStringabcdefghijklmno

Follow the instructions on https://developers.google.com/analytics/resources/articles/gdata-migration-guide (Create a Project in the Google APIs Console) to generate your key Once you have it set it as part of the querystring to request to Google Analytics service, in this case: YourAPIkEStringabcdefghijklmno

要获取 profileId(代码中的 ID),您应该这样做:

登录您的分析帐户,在列表中选择所需的域(蓝色链接)单击管理员按钮,然后在配置文件选项卡上找到配置文件配置子选项卡,就在那里,您将在本例中找到配置文件 ID,即 8 个字符长的 ID:12345678

Log into your analytics account, select the desired domain on your list (blue link) click on the administrator button and on the profiles tab find the profile configuration subtab, right there you will find the profile id in this case the eight characters long id: 12345678

这里有一些 C# 代码可以帮助您获取该 ID 的访问次数:

public string VisitsNumber() 
    {
        string visits = string.Empty;
        string username = "youremailuser@domain.com";
        string pass = "yourpassword";
        string gkey = "?key=YourAPIkEYYourAPIkEYYourAPIkEYYourAPIkE";

    string dataFeedUrl = "https://www.google.com/analytics/feeds/data" + gkey;
    string accountFeedUrl = "https://www.googleapis.com/analytics/v2.4/management/accounts" + gkey;

    AnalyticsService service = new AnalyticsService("WebApp");
    service.setUserCredentials(username, pass);

    DataQuery query1 = new DataQuery(dataFeedUrl);

    query1.Ids = "ga:12345678";
    query1.Metrics = "ga:visits";
    query1.Sort = "ga:visits";

    //You were setting 2013-09-01 and thats an invalid date because it hasn't been reached yet, be sure you set valid dates
    //For start date is better to place an aprox date when you registered the domain on Google Analytics for example January 2nd 2012, for an end date the actual date is enough, no need to go further
    query1.GAStartDate = new DateTime(2012, 1, 2).ToString("yyyy-MM-dd"); 
    query1.GAEndDate = DateTime.Now.ToString("yyyy-MM-dd");
    query1.StartIndex = 1;        

    DataFeed dataFeedVisits = service.Query(query1);

    foreach (DataEntry entry in dataFeedVisits.Entries)
    {
        string st = entry.Title.Text;
        string ss = entry.Metrics[0].Value;
        visits = ss;
    }

    return visits;
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) 
    {
        Response.Write("Visits:" + this.VisitsNumber());
    }
}

由于 2.4 API 不再那么灵活,我在这里有另一篇文章破解它以获取配置文件 ID:从注册中获取特定 ProfileId使用 GData .NET Analytics API 2.4 的帐户如果您需要将代码转换为 C#,您可以使用 Telerik 转换器:http://converter.telerik.com/

Since the 2.4 API is not so flexible anymore, I have another post here hacking it to get the profile Id: Getting an specific ProfileId from registered Accounts using GData .NET Analytics API 2.4 if you need to convert the code to C# you can use the Telerik converter: http://converter.telerik.com/

我认为这足以使用 2.4 API.如果您需要额外帮助,请告诉我.

I think this suffice to use the 2.4 API. If you need extra help let me know.

这篇关于C# 中的 Google Analytics API - 请求执行失败:https://www.google.com/analytics/feeds/accounts/default的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆