private void beginAuthorizationButton_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(this.googleTokenManager.ConsumerKey)) { MessageBox.Show(this, "You must modify the App.config or OAuthConsumerWpf.exe.config file for this application to include your Google OAuth consumer key first.", "Configuration required", MessageBoxButton.OK, MessageBoxImage.Stop); return; } var auth = new Authorize( this.google, (DesktopConsumer consumer, out string requestToken) => GoogleConsumer.RequestAuthorization( consumer, GoogleConsumer.Applications.Contacts | GoogleConsumer.Applications.Blogger, out requestToken)); bool?result = auth.ShowDialog(); if (result.HasValue && result.Value) { this.googleAccessToken = auth.AccessToken; this.postButton.IsEnabled = true; XDocument contactsDocument = GoogleConsumer.GetContacts(this.google, this.googleAccessToken, 25, 1); var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")) select new { Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value, Email = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value }; this.contactsGrid.Children.Clear(); foreach (var contact in contacts) { this.contactsGrid.RowDefinitions.Add(new RowDefinition()); TextBlock name = new TextBlock { Text = contact.Name }; TextBlock email = new TextBlock { Text = contact.Email }; Grid.SetRow(name, this.contactsGrid.RowDefinitions.Count - 1); Grid.SetRow(email, this.contactsGrid.RowDefinitions.Count - 1); Grid.SetColumn(email, 1); this.contactsGrid.Children.Add(name); this.contactsGrid.Children.Add(email); } } }
private void InitializeGoogleConsumer() { this.googleTokenManager.ConsumerKey = ConfigurationManager.AppSettings["googleConsumerKey"]; this.googleTokenManager.ConsumerSecret = ConfigurationManager.AppSettings["googleConsumerSecret"]; string pfxFile = ConfigurationManager.AppSettings["googleConsumerCertificateFile"]; if (string.IsNullOrEmpty(pfxFile)) { this.google = new DesktopConsumer(GoogleConsumer.ServiceDescription, this.googleTokenManager); } else { string pfxPassword = ConfigurationManager.AppSettings["googleConsumerCertificatePassword"]; var signingCertificate = new X509Certificate2(pfxFile, pfxPassword); var service = GoogleConsumer.CreateRsaSha1ServiceDescription(signingCertificate); this.google = new DesktopConsumer(service, this.googleTokenManager); } }
protected void Page_Load(object sender, EventArgs e) { try { //NOTE: removed. so every page will provide google auth dialog if (AccessToken != null) { //Previously stored token. clean it ImportConfiguration.GoogleTokenManager.ExpireToken(AccessToken); } //Authenticating when loading pages if (!IsPostBack) { using ( var google = new WebConsumer(GoogleConsumer.ServiceDescription, ImportConfiguration.GoogleTokenManager)) { // Is Google calling back with authorization? var accessTokenResponse = google.ProcessUserAuthorization(); if (accessTokenResponse != null) { AccessToken = accessTokenResponse.AccessToken; //Redirecting to result page SubmitToken(AccessToken, Source); } else { // If we don't yet have access, immediately request it. GoogleConsumer.RequestAuthorization(google, "https://docs.google.com/feeds/ https://spreadsheets.google.com/feeds/ https://docs.googleusercontent.com/"); } } } } catch (ThreadAbortException) { } catch (Exception ex) { SubmitError(ex.Message, Source); } }
protected void getAddressBookButton_Click(object sender, EventArgs e) { var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager); XDocument contactsDocument = GoogleConsumer.GetContacts(google, this.AccessToken); var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")) select new { Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value, Email = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value }; StringBuilder tableBuilder = new StringBuilder(); tableBuilder.Append("<table><tr><td>Name</td><td>Email</td></tr>"); foreach (var contact in contacts) { tableBuilder.AppendFormat( "<tr><td>{0}</td><td>{1}</td></tr>", HttpUtility.HtmlEncode(contact.Name), HttpUtility.HtmlEncode(contact.Email)); } tableBuilder.Append("</table>"); resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() }); }
protected void Page_Load(object sender, EventArgs e) { if (this.TokenManager != null) { MultiView1.ActiveViewIndex = 1; if (!IsPostBack) { var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager); // Is Google calling back with authorization? var accessTokenResponse = google.ProcessUserAuthorization(); if (accessTokenResponse != null) { this.AccessToken = accessTokenResponse.AccessToken; } else if (this.AccessToken == null) { // If we don't yet have access, immediately request it. GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts); } } } }
private void GetDocuments(string endpoint, List <Document> result) { const string ns = "{http://www.w3.org/2005/Atom}"; var root = GoogleConsumer.GetDocList(_consumer, _accessToken, endpoint).Root; if (root == null) { return; } foreach (var element in root.Elements(ns + "entry")) { var entry = new Document(); var kind = element.Elements(ns + "category") .Where(c => c.AttributeValueOrDefault("scheme") == "http://schemas.google.com/g/2005#kind") .Select(c => c.AttributeValueOrDefault("label")) .FirstOrDefault(); entry.IsFolder = kind == "folder"; entry.Title = element.ElementValueOrDefault(ns + "title", "unnamed"); if (!entry.IsFolder) { entry.Title += GetExtension(kind, entry.Title); } if (!entry.IsFolder && Global.EnableUploadFilter && !FileUtility.ExtsUploadable.Contains(FileUtility.GetFileExtension(entry.Title), _comparer)) { continue; } entry.Id = GetId(element.ElementValueOrDefault(ns + "id")); if (!string.IsNullOrEmpty(entry.Id) && element.Element(ns + "content") != null) { entry.ContentLink = element.Element(ns + "content").AttributeValueOrDefault("src") + GetExport(entry.Title); entry.CreateBy = element.Element(ns + "author") != null ? element.Element(ns + "author").ElementValueOrDefault(ns + "name", "noname") : "noname"; entry.Parent = element.Elements(ns + "link") .Where( c => c.Attribute("rel") != null && c.AttributeValueOrDefault("rel") == "http://schemas.google.com/docs/2007#parent") .Select(c => GetId(c.AttributeValueOrDefault("href"))) .OrderBy(c => c) .FirstOrDefault(); var published = element.ElementValueOrDefault(ns + "published"); DateTime date; if (DateTime.TryParse(published, out date)) { entry.CreateOn = date; } result.Add(entry); } } var next = root.Elements(ns + "link") .Where(e => e.Attribute("rel") != null && e.AttributeValueOrDefault("rel") == "next") .Select(e => e.AttributeValueOrDefault("href")) .FirstOrDefault(); if (next != null) { GetDocuments(next, result); } }
public Stream GetDocumentStream(string contentLink, out long size) { return(GoogleConsumer.GetDoc(_consumer, _accessToken, contentLink, out size)); }
private void postButton_Click(object sender, RoutedEventArgs e) { XElement postBodyXml = XElement.Parse(this.postBodyBox.Text); GoogleConsumer.PostBlogEntry(this.google, this.googleAccessToken, this.blogUrlBox.Text, this.postTitleBox.Text, postBodyXml); }
/// <summary> /// Returns user email from google, using <see cref="GoogleConsumer.GetUserEmail"/> method. /// </summary> /// <param name="credentials">User oauth credentials.</param> /// <returns>User email from google</returns> protected virtual string GetUserEmailFromGoogle(OAuthUserCredentials credentials) { return(GoogleConsumer.GetUserEmail(credentials.AccessToken)); }