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

浪尽此生 提交于 2019-12-03 09:00:58
CoderRoller

Analytics Account:

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/

To get your API Key do this:

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

To get the profileId (Ids on the code) you should do this:

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

Here you have some C# code to help you getting the number of visits for that 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());
    }
}

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/

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!