title: 401 (Unauthorized) Error with DotNetOpenAuth and Google Calendar
body: <p>I've been tinkering for a while now with getting the Google Calendar API working with DotNetOpenAuth.  I've tried going about it in a few different ways, and I keep running up against the following error:</p>
  
  <pre><code>The remote server returned an error: (401) Unauthorized.
  </code></pre>
  
  <p>I've tried it both by using the CalendarService in the Google Data API for .NET, and by adapting the Google Contacts sample in the DotNetOpenAuth samples project.</p>
  
  <p>When I tried using the CalendarService, I set up my service for authentication by setting the authentication token like so:</p>
  
  <pre><code>_service = new CalendarService("CalendarSampleApp");
  _service.SetAuthenticationToken(accessToken);
  </code></pre>
  
  <p>When reworking the Google Contacts sample, I simply changed the endpoint to reflect the Google Calendar endpoint instead of the Google Contacts endpoint, like so:</p>
  
  <pre><code>private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("http://www.google.com/calendar/feeds/default/private/full", HttpDeliveryMethods.GetRequest);
  
  public static XDocument GetCalendarEntries(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/)
  {
      if (consumer == null)
      {
          throw new ArgumentNullException("consumer");
      }
  
      var extraData = new Dictionary&lt;string, string&gt;() {
          { "start-index", startIndex.ToString(CultureInfo.InvariantCulture) },
          { "max-results", maxResults.ToString(CultureInfo.InvariantCulture) },
      };
      var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken, extraData);
  
      // Enable gzip compression.  Google only compresses the response for recognized user agent headers. - Mike Lim
      request.AutomaticDecompression = DecompressionMethods.GZip;
      request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16";
  
      var response = consumer.Channel.WebRequestHandler.GetResponse(request);
      string body = response.GetResponseReader().ReadToEnd();
      XDocument result = XDocument.Parse(body);
      return result;
  }
  </code></pre>
  
  <p>When I run this, here's the request that gets sent:</p>
  
  <p><a href="http://www.google.com/calendar/feeds/default/private/full?oauth_token=1/kyuMER6kPiG13pOw5M4NoEE7AJdN2fE4jTKSgaMToT8&amp;oauth_consumer_key=419892106817-obg9lu9j9ihpnlh2e2qa01ber4hmq5mh.apps.googleusercontent.com&amp;oauth_nonce=NtfQzRV3&amp;oauth_signature_method=HMAC-SHA1&amp;oauth_signature=XEdf4ipECY2iBfdQfupD1IK1uFw%3D&amp;oauth_version=1.0&amp;oauth_timestamp=1358213228&amp;start-index=1&amp;max-results=5&amp;gsessionid=TV7D3zHTc7T3l41Xqfn-ig" rel="nofollow">http://www.google.com/calendar/feeds/default/private/full?oauth_token=1/kyuMER6kPiG13pOw5M4NoEE7AJdN2fE4jTKSgaMToT8&amp;oauth_consumer_key=419892106817-obg9lu9j9ihpnlh2e2qa01ber4hmq5mh.apps.googleusercontent.com&amp;oauth_nonce=NtfQzRV3&amp;oauth_signature_method=HMAC-SHA1&amp;oauth_signature=XEdf4ipECY2iBfdQfupD1IK1uFw%3D&amp;oauth_version=1.0&amp;oauth_timestamp=1358213228&amp;start-index=1&amp;max-results=5&amp;gsessionid=TV7D3zHTc7T3l41Xqfn-ig</a></p>
  
  <p>As I said, in either of these cases, I'm getting the same 401 error.  Does anyone have any ideas on what I am doing wrong, or how to troubleshoot further?</p>