title: how to solve 403 error in google analytics api call
body: <p>I'm using the code in the following post:
  <a href="https://stackoverflow.com/questions/6806577/google-analytics-api-programmatically-fetch-page-views-in-server-side">Google Analytics API - Programmatically fetch page views in server side</a></p>
  
  <p>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 ?</p>
  
  <pre><code>//-------------- 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}&amp;dimensions={2}&amp;metrics={3}&amp;sort={4}&amp;start-date={5}&amp;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 =&gt; token.StartsWith("Auth=")).Single();
  }
  </code></pre>