title: Why am I getting a 401 (Unauthorized) error when POSTing to Google Reader API?
body: <p>I'm trying to interface with the Google Reader (undocumented/unofficial) API using information from <a href="http://code.google.com/p/pyrfeed/wiki/GoogleReaderAPI" rel="nofollow noreferrer">this page</a>. My first step is to get a SID and token, which works fine, but I can't seem to POST anything without getting a 401 error. </p>
  
  <p>Here is the code I'm using to get my SID and token:</p>
  
  <pre><code>static string getSid()
      {
          HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.google.com/accounts/ClientLogin?service=reader&amp;Email=username&amp;Passwd=password");
          req.Method = "GET";
          string sid;
  
          HttpWebResponse response = (HttpWebResponse)req.GetResponse();
          using (var stream = response.GetResponseStream())
          {
              StreamReader r = new StreamReader(stream);
              string resp = r.ReadToEnd();
  
              int indexSid = resp.IndexOf("SID=") + 4;
              int indexLsid = resp.IndexOf("LSID=");
              sid = resp.Substring(indexSid, indexLsid - 5);
  
              return sid;
          }
      }
  </code></pre>
  
  <p>And to generate a cookie and get the token:</p>
  
  <pre><code>    static Cookie getCookie(string sid)
      {
          Cookie cookie = new Cookie("SID", sid, "/", ".google.com");
          return cookie;
      }
  
      static string getToken(string sid, Cookie cookie)
      {
          string token;
          string url = "http://www.google.com/reader/api/0/token";
  
          HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
          req.CookieContainer = new CookieContainer();
          req.CookieContainer.Add(cookie);
  
          HttpWebResponse response = (HttpWebResponse)req.GetResponse();
          using (var stream = response.GetResponseStream())
          {
              StreamReader r = new StreamReader(stream);
              token = r.ReadToEnd();
              return token;
          }
      }
  </code></pre>
  
  <p>Now if I try to do a POST (in this example, insert a new tag) using the following, I get the 401 error.</p>
  
  <pre><code>    static void insertTag(string tag, Cookie cookie)
      {
          string result = "";
          string data = Uri.EscapeDataString("a="+tag+"&amp;T=" + Program.token);
  
          byte[] buffer = Encoding.GetEncoding(1252).GetBytes(data);
          HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create
              ("http://www.google.com/reader/api/0/edit-tag?client=-");
          WebReq.Method = "POST";
  
          WebReq.Credentials = new NetworkCredential("username", "password");
  
  
          WebReq.ContentType = "application/x-www-form-urlencoded";
          WebReq.ContentLength = buffer.Length;
  
          Stream PostData = WebReq.GetRequestStream();
  
          PostData.Write(buffer, 0, buffer.Length);
          PostData.Close();
  
          HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
          Stream Answer = WebResp.GetResponseStream();
          StreamReader _Answer = new StreamReader(Answer);
          result = _Answer.ReadToEnd();
  
          if (result.Length &lt; 0)
              result = "";
      }
  </code></pre>
  
  <p>The error occurs on the line <code>Stream Answer = WebResp.GetResponseStream();</code></p>