3

I'm using the code in the following post: Google Analytics API - Programmatically fetch page views in server side

but getting a 403 forbidden error on the highlighted line below. I don't think it's a credential issue, becuase my credentials are correct, as I have checked and double checked, and also I log in to the analytics account with these credentials. So maybe it is somekind of folder permissions issue ?

//-------------- Get Auth Token -------------------

WebClient webClient = new WebClient();
NameValueCollection data = new NameValueCollection();
data.Add("accountType", "GOOGLE");
data.Add("Email", "xxxx@gmail.com");
data.Add("Passwd", "xxxx");//Passwd, not a misspell.
data.Add("service", "analytics");
data.Add("source", "xxxx-xxxx-xx");//Could be anything.

byte[] bytes = webClient.UploadValues("https://www.google.com/accounts/ClientLogin", "POST", data);
string tokens = Encoding.UTF8.GetString(bytes);
string authToken = extractAuthToken(tokens);

//-------------- Get page views -------------------

string feed = "https://www.google.com/analytics/feeds/data";

//Required:
string ids = "ga:xxxx";
string metrics = "ga:pageviews";
string startDate = "2011-06-25";
string endDate = "2011-07-25";

//Optional:
string dimensions = "ga:pagePath";
string sort = "-ga:pageviews";            

string feedUrl = string.Format("{0}?ids={1}&dimensions={2}&metrics={3}&sort={4}&start-date={5}&end-date={6}",
    feed, ids, dimensions, metrics, sort, startDate, endDate);

webClient.Headers.Add("Authorization", "GoogleLogin " + authToken);

// This is the line I get the 403 error on:
**string result = webClient.DownloadString(feedUrl);**

//-------------- Extract data from xml -------------------

XDocument xml = XDocument.Parse(result);
var ns1 = "{http://www.w3.org/2005/Atom}";
var ns2 = "{http://schemas.google.com/analytics/2009}";

var q = from entry in xml.Descendants()
        where entry.Name == ns1 + "entry"
        select new
        {
            PagePath = entry.Element(ns2 + "dimension").Attribute("value").Value,
            Views = entry.Element(ns2 + "metric").Attribute("value").Value
        };

//-------------- Do something with data -------------------
foreach (var page in q)
{
    Debug.WriteLine(page.PagePath + " " + page.Views);                
}

//-------------- Help Method -------------------
private string extractAuthToken(string data)
{          
    var tokens = data.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);            
    return tokens.Where(token => token.StartsWith("Auth=")).Single();
}
Community
  • 1
  • 1
user560498
  • 537
  • 1
  • 16
  • 25
  • Its a credential issue. Google is not able to authenticate you hence you are getting forbidden response. Other likely reason can be if you /your team has already consumed max API Call limit for the given account. – codersofthedark Dec 06 '11 at 17:53
  • In case you can't use this code even once... I've copy pasted your code and tested it with my credentials and works fine, are you sure your ga id and dates are correct? – varholl Dec 06 '11 at 18:21
  • I'm able to use this code from localhost but when I run it from production environment (tried on many dedicated servers I have access to that have NOT exceed the daily quota for sure), and I get the 403 Forbidden message with the same exact code on production. Anyone know why that occurs?? – Code Monkey Jun 28 '13 at 17:11

1 Answers1

0

If you call the Google Analytics API too frequently, you could get 403 Forbidden errors. From that link:

General Analytics API quotas. These apply to both the Analytics APIs, i.e., Management API and Core Reporting API:
- 50,000 requests per project per day
- 10 queries per second (QPS) per IP

I've seen 403 errors returned from the AdWords API when my applications have made too many consecutive calls, so that potentially could be the cause of your problem.

EDIT

If you're not able to make any calls at all, then review the steps listed here under "Before You Begin". According to the documentation, you'll need to register your application through the Google API console before you can use the API.

rsbarro
  • 27,021
  • 9
  • 71
  • 75
  • Thanks... may I ask how this can be resolved ? (I started using this code only today, so why is this hapenning ?) – user560498 Dec 06 '11 at 18:01
  • oops! just realised that I haven't registered to google api, so that may be the cause of my problem, will check and post if solved ... – user560498 Dec 06 '11 at 18:05
  • Thanks @rsbarro for sharing. However, in Google API console, one can only register for OAuth 2.0 access. How can it relate/ help ClientLogin access? – ericn Feb 22 '13 at 05:52