private void GoogleServicesOnFailedToAuthenticate(object o, EventArgs eventArgs) { this.GMailServices = null; this.CalendarServices = null; this.Authenticate(); }
private async void Authenticate() { var SpotifyUrl = $"https://accounts.google.com/o/oauth2/auth?client_id={this.clientId}&redirect_uri={Uri.EscapeDataString(this.redirectURI)}&response_type=code&scope={Uri.EscapeDataString(this.scope)}"; var StartUri = new Uri(SpotifyUrl); var EndUri = new Uri(redirectURI); WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, StartUri, EndUri); if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { var decoder = new WwwFormUrlDecoder(new Uri(WebAuthenticationResult.ResponseData).Query); if (decoder[0].Name != "code") { System.Diagnostics.Debug.WriteLine($"OAuth authorization error: {decoder.GetFirstValueByName("error")}."); return; } var autorizationCode = decoder.GetFirstValueByName("code"); var pairs = new Dictionary <string, string>(); pairs.Add("code", autorizationCode); pairs.Add("client_id", this.clientId); pairs.Add("client_secret", this.clientSecret); pairs.Add("redirect_uri", this.redirectURI); pairs.Add("grant_type", "authorization_code"); var formContent = new Windows.Web.Http.HttpFormUrlEncodedContent(pairs); var client = new Windows.Web.Http.HttpClient(); var httpResponseMessage = await client.PostAsync(new Uri("https://www.googleapis.com/oauth2/v4/token"), formContent); if (!httpResponseMessage.IsSuccessStatusCode) { System.Diagnostics.Debug.WriteLine($"OAuth authorization error: {httpResponseMessage.StatusCode}."); return; } string jsonString = await httpResponseMessage.Content.ReadAsStringAsync(); var jsonObject = Windows.Data.Json.JsonObject.Parse(jsonString); var accessToken = jsonObject["access_token"].GetString(); this.accessToken = accessToken; this.gotAccessToken = true; this.GMailServices = new GmailServices(this.accessToken); this.GMailServices.LatestEmailsEvent += (sender, status) => { this.LatestEmailsEvent?.Invoke(this, status); }; this.CalendarServices = new CalendarServices(this.accessToken); this.CalendarServices.LatestCalendarEvent += (sender, status) => { this.LatestCalendarEvent?.Invoke(this, status); }; } }
public GoogleServices() { this.GMailServices = new GmailServices(this.accessToken); this.GMailServices.FailedToAuthenticate += this.GoogleServicesOnFailedToAuthenticate; this.GMailServices.LatestEmailsEvent += (sender, status) => { this.LatestEmailsEvent?.Invoke(this, status); }; this.CalendarServices = new CalendarServices(this.accessToken); this.CalendarServices.LatestCalendarEvent += (sender, status) => { this.LatestCalendarEvent?.Invoke(this, status); }; this.CalendarServices.FailedToAuthenticate += this.GoogleServicesOnFailedToAuthenticate; //this.Authenticate(); }