Created
September 22, 2009 08:36
-
-
Save davetheninja/190928 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
namespace Core | |
{ | |
public class GoogleTokenQuery | |
{ | |
const string AuthenticationUrlFormat = "accountType=GOOGLE&Email={0}&Passwd={1}&source=davetheninja.net&service=analytics"; | |
public string GetToken(string email, string password) | |
{ | |
if (string.IsNullOrEmpty(email)) | |
throw new ArgumentException("Google email not specified."); | |
if (string.IsNullOrEmpty(password)) | |
throw new ArgumentException("Google password not specified."); | |
var authenticationBody = BuildAuthenticationBody(email, password); | |
var httpWebRequest = WebRequest.Create("https://www.google.com/accounts/ClientLogin"); | |
httpWebRequest.Method = "POST"; | |
httpWebRequest.ContentType = "application/x-www-form-urlencoded"; | |
string token; | |
try | |
{ | |
var stream = httpWebRequest.GetRequestStream(); | |
using (var streamWriter = new StreamWriter(stream)) | |
{ | |
streamWriter.Write(authenticationBody); | |
} | |
var httpResponse = httpWebRequest.GetResponse(); | |
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) | |
{ | |
var tokenResponse = streamReader.ReadToEnd().Split(new[] { "\n" }, StringSplitOptions.RemoveEmptyEntries); | |
token = tokenResponse.Where(t => t.StartsWith("Auth=")).FirstOrDefault().Replace("Auth=", ""); | |
} | |
} | |
catch (WebException ex) | |
{ | |
throw new GoogleApiException(ex.Message); | |
} | |
return token; | |
} | |
static string BuildAuthenticationBody(string email, string password) | |
{ | |
return string.Format(AuthenticationUrlFormat, email, password); | |
} | |
} | |
} | |
using System.Collections.Generic; | |
using Google.Analytics; | |
using Google.GData.Client; | |
namespace Core | |
{ | |
public class GoogleAccountQuery : IGoogleAccountQuery | |
{ | |
readonly string _email; | |
readonly string _password; | |
readonly string _applicationName; | |
public GoogleAccountQuery(string email, string password, string applicationName) | |
{ | |
_email = email; | |
_password = password; | |
_applicationName = applicationName; | |
} | |
public IList<GoogleAnalyticsAccount> GetAccounts() | |
{ | |
var token = new GoogleTokenQuery().GetToken(_email, _password); | |
var credentials = new GDataCredentials(token); | |
var requestSettings = new RequestSettings(_applicationName, credentials); | |
var accountsFeed = new AnalyticsRequest(requestSettings).GetAccounts(); | |
var accounts = new List<GoogleAnalyticsAccount>(); | |
accountsFeed.Entries.Each(x => accounts.Add(new GoogleAnalyticsAccount(x))); | |
return accounts; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment