在要求C#-Execution谷歌Analytics(分析)API失败:https://www.go

2019-07-31 18:38发布

我想要访问谷歌分析数据,我得到了来自谷歌数据API SDK样本。 但这些编码不工作,并抛出异常

请求执行失败: https://www.google.com/analytics/feeds/accounts/default

所以我发现这样做的原因是谷歌更新,这是对V3.0。 我搜索了C#最新的编码,但我无法找到这个解决方案。

我有同样的问题,因为这一点,但与C#。 异常使用的GData .NET分析API时抛出

我试图做如下改变,因为它在谷歌开发人员表示编码- 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;       
}

但即使我改变了它抛出例外

数据馈送数据= 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

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

我的问题:

  1. 我怎样才能纠正这一错误?

  2. 如何访问谷歌分析数据? 它是否正确? 否则什么办法做这件事? 一个例子我想提供简档和它们的值。 (标题和页面浏览量)

Answer 1:

Analytics帐户:

我假设你有一个Analytics帐户已如果你不然后创建一个,并在这里注册您的域名: http://www.google.com/intl/en/analytics/

为了让您的API密钥做到这一点:

按照上的说明https://developers.google.com/analytics/resources/articles/gdata-migration-guide (创建于谷歌API控制台项目),一旦你把它设置为查询字符串的一部分来生成你的密钥要求谷歌分析服务,在这种情况下:YourAPIkEStringabcdefghijklmno

为了让简档(IDS上的代码),你应该这样做:

登录到您的Analytics帐户,选择列表上的所需域(链接),点击管理员按钮和配置文件选项卡上找到的配置文件配置子选项卡,在那里你会发现在这种情况下,八个字符标识的配置文件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: 获取使用的GData .NET分析API 2.4注册帐户的特定简档 ,如果你需要将代码转换为C#中,您可以使用Telerik的转换器: http://converter.telerik.com/

我认为这足以使用2.4 API。 如果你需要额外的帮助,让我知道。



文章来源: Google Analytics API in C# -Execution of request failed: https://www.google.com/analytics/feeds/accounts/default