views:

96

answers:

1

I have been racking my brain for a few days trying to get a list of calendars from Google using DotNetOpenAuth.

I can successfully get a list of contacts using the DotNetOpenAuth Samples. I have integrated it with my domain using the OpenId+OAuth. Everything works great to get a list of contacts.

So from there I modified the code to try to retrieve a list of Calendars and I keep getting a 401 Unauthorized error.

I know it is authorizing because I can get the contact list. Does anyone have a code example how they are retrieving calendars or calendar events using the DotNetOpenAuth with Google???

Thanks

Update:

Thanks for the response. I have read everything I can get my hands on. Here is what I have done so far

Step 1: I created a new GetCalendarEndPoint in the GoogleConsumer.cs

private static readonly MessageReceivingEndpoint GetCalendarEndpoint = new MessageReceivingEndpoint("https://www.google.com/calendar/feeds/default", HttpDeliveryMethods.GetRequest);

Step 2: Next I created a new method GetCalendars patterned after the GetContacts Method in GoogleConsumer.cs - (Rebuilt the dll etc.)

    public static XDocument GetCalendars(ConsumerBase consumer, string accessToken, int maxResults/* = 25*/, int startIndex/* = 1*/) {
            if (consumer == null)
            {
                throw new ArgumentNullException("consumer");
            }

            var request = consumer.PrepareAuthorizedRequest(GetCalendarEndpoint, accessToken);
            var response = consumer.Channel.WebRequestHandler.GetResponse(request);
            string body = response.GetResponseReader().ReadToEnd();
            XDocument result = XDocument.Parse(body);
            return result;

Step 3: In my Application I modified the ScopeURI to the the Calendar URI from GoogleConsumer as follows

private IAuthenticationRequest GetGoogleRequest()
{
    Realm realm = Request.Url.Scheme + Uri.SchemeDelimiter + Global.GoogleTokenManager.ConsumerKey + "/";
    IAuthenticationRequest authReq = relyingParty.CreateRequest(GoogleOPIdentifier, realm);

    // Prepare the OAuth extension
    string scope = GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Calendar);
    Global.GoogleWebConsumer.AttachAuthorizationRequest(authReq, scope);

    // We also want the user's email address
    var fetch = new FetchRequest();
    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
    authReq.AddExtension(fetch);

    return authReq;
}

However, when I run the app I get 401 Unauthorized when I make the following call

 var calendars = GoogleConsumer.GetCalendars(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);

I have also checked that the State.GoogleAccess token exists by simply displaying it on my screen before I trigger the method that makes this call.

Again, if I exectute

var calendars = GoogleConsumer.GetContacs(Global.GoogleWebConsumer, State.GoogleAccessToken, 25, 1);

then it works??????? Thanks for you help.

A: 

Have you read the Google Calendar data API documentation to make sure you have the right endpoints programmed in? Have you also modified the code that acquires the access token to request access to Google Calendar in addition to Google Contacts? The access token in the sample only gets Contacts permissions unless you change it.

Andrew Arnott
Would it possibly have to do with the way we are creating the request since the end point for Contacts is HTTP and the end point for the Calendar is HTTPS?
Brett
Absolutely that could be it. And of course the creation of MessageReceivingEndpoint is code you control so you can adjust that.
Andrew Arnott
Can you see anything in my code I am doing wrong or point me in the right direction. This is really stumping me.
Brett