//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.ApplicationBlock {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth;
using DotNetOpenAuth.OAuth.ChannelElements;
///
/// A consumer capable of communicating with Google Data APIs.
///
public static class GoogleConsumer {
///
/// The Consumer to use for accessing Google data APIs.
///
private static readonly ServiceProviderDescription GoogleDescription = new ServiceProviderDescription {
RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetRequestToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthAuthorizeToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.google.com/accounts/OAuthGetAccessToken", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest),
TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
};
///
/// A mapping between Google's applications and their URI scope values.
///
private static readonly Dictionary DataScopeUris = new Dictionary {
{ Applications.Contacts, "http://www.google.com/m8/feeds/" },
{ Applications.Calendar, "http://www.google.com/calendar/feeds/" },
};
///
/// The URI to get contacts once authorization is granted.
///
private static readonly MessageReceivingEndpoint GetContactsEndpoint = new MessageReceivingEndpoint("http://www.google.com/m8/feeds/contacts/default/full/", HttpDeliveryMethods.GetRequest);
///
/// The many specific authorization scopes Google offers.
///
[Flags]
public enum Applications : long {
///
/// The Gmail address book.
///
Contacts = 0x1,
///
/// Appointments in Google Calendar.
///
Calendar = 0x2,
}
///
/// Initializes a new instance of the class that is prepared to communicate with Google.
///
/// The token manager.
/// The consumer key.
/// The newly instantiated .
public static WebConsumer CreateWebConsumer(ITokenManager tokenManager, string consumerKey) {
return new WebConsumer(GoogleDescription, tokenManager) {
ConsumerKey = consumerKey,
};
}
///
/// Initializes a new instance of the class that is prepared to communicate with Google.
///
/// The token manager.
/// The consumer key.
/// The newly instantiated .
public static DesktopConsumer CreateDesktopConsumer(ITokenManager tokenManager, string consumerKey) {
return new DesktopConsumer(GoogleDescription, tokenManager) {
ConsumerKey = consumerKey,
};
}
///
/// Requests authorization from Google to access data from a set of Google applications.
///
/// The Google consumer previously constructed using or .
/// The requested access scope.
public static void RequestAuthorization(WebConsumer consumer, Applications requestedAccessScope) {
if (consumer == null) {
throw new ArgumentNullException("consumer");
}
var extraParameters = new Dictionary {
{ "scope", GetScopeUri(requestedAccessScope) },
};
Uri callback = Util.GetCallbackUrlFromContext();
var request = consumer.PrepareRequestUserAuthorization(callback, extraParameters, null);
consumer.Channel.Send(request);
}
///
/// Requests authorization from Google to access data from a set of Google applications.
///
/// The Google consumer previously constructed using or .
/// The requested access scope.
/// The unauthorized request token assigned by Google.
/// The request token
public static Uri RequestAuthorization(DesktopConsumer consumer, Applications requestedAccessScope, out string requestToken) {
if (consumer == null) {
throw new ArgumentNullException("consumer");
}
var extraParameters = new Dictionary {
{ "scope", GetScopeUri(requestedAccessScope) },
};
return consumer.RequestUserAuthorization(extraParameters, null, out requestToken);
}
///
/// Gets the Gmail address book's contents.
///
/// The Google consumer previously constructed using or .
/// The access token previously retrieved.
/// An XML document returned by Google.
public static XDocument GetContacts(ConsumerBase consumer, string accessToken) {
if (consumer == null) {
throw new ArgumentNullException("consumer");
}
var response = consumer.PrepareAuthorizedRequestAndSend(GetContactsEndpoint, accessToken);
string body = response.GetResponseReader().ReadToEnd();
XDocument result = XDocument.Parse(body);
return result;
}
///
/// Gets the scope URI in Google's format.
///
/// The scope, which may include one or several Google applications.
/// A space-delimited list of URIs for the requested Google applications.
private static string GetScopeUri(Applications scope) {
return string.Join(" ", Util.GetIndividualFlags(scope).Select(app => DataScopeUris[(Applications)app]).ToArray());
}
}
}