Reputation: 1315
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:
The remote server returned an error: (401) Unauthorized.
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.
When I tried using the CalendarService, I set up my service for authentication by setting the authentication token like so:
_service = new CalendarService("CalendarSampleApp");
_service.SetAuthenticationToken(accessToken);
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:
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<string, string>() {
{ "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;
}
When I run this, here's the request that gets sent:
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?
Upvotes: 1
Views: 1337
Reputation: 3512
I think you are working with an old version of the library. Please download the latest stable release from: https://code.google.com/p/google-api-dotnet-client/wiki/Downloads
And you should also consider reading OAuth2's documentation: https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2
Hope it may help, Eyal
Upvotes: 1