async void OnLoginButtonClicked(object sender, EventArgs e) { var user = new User { Username = usernameEntry.Text, Password = passwordEntry.Text }; //var isValid = AreCredentialsCorrect(user); accessToken = await OAuth.GetAccessToken(user.Username, user.Password); if (accessToken != null) { CouponViewModels data = await OAuth.GetCoupon(accessToken); App.IsUserLoggedIn = true; Navigation.InsertPageBefore(new MainPage(), this); await Navigation.PopAsync(); } else { messageLabel.Text = "Login failed"; passwordEntry.Text = string.Empty; } }
public static async Task <CouponViewModels> GetCoupon(string accessToken) { CouponViewModels data = new CouponViewModels(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(baseUri); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Add the Autorization header with the AccessToken client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); // ceate the URL string. string url = string.Format("api/CouponsNew"); // make the request HttpResponseMessage response = await client.GetAsync(url); // parse the response and return the data. string jsonSting = await response.Content.ReadAsStringAsync(); data = JsonConvert.DeserializeObject <CouponViewModels>(jsonSting); return(data); } }