public async Task <ActionResult> Callback(string code) { var authenticator = new OAuth2Helper(_appCredentials, ConfigurationManager.AppSettings["BaseUrl"] + "/band/callback"); OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); if (!string.IsNullOrEmpty(accessToken.Token)) { _bandClient = GetBandClient(accessToken); } return(RedirectToAction("Profile")); }
//Final step. Take this authorization information and use it in the app /// <summary> /// Part 2 of authorisation. User has (hopefully) accepted request and fitbit passed authorisation code. /// </summary> /// <returns></returns> public async Task <ActionResult> Callback() { FitbitAppCredentials appCredentials = FitbitHelper.GetFitbitAppCredentials(); var authenticator = new OAuth2Helper(appCredentials, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback"); string code = Request.Params["code"]; // ask for access token. OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); // save token (user may have previously authorised in which case update) FitbitHelper.AddOrUpdateUser(_unitOfWork, User.Identity.GetUserId(), accessToken); return(View("Home", PopulateModel())); }
private void Load() { try { OAuth2Helper authenticator = new OAuth2Helper("227H9T", "74e4a047fce979dd36a0edbd626c3939", "http://Miriot.suismoi.fr"); string[] scopes = new[] { "profile", "weight" }; string authUrl = authenticator.GenerateAuthUrl(scopes, null); WebView browser = new WebView(); browser.Height = 300; browser.Width = 300; browser.Source = new Uri(authUrl); browser.DefaultBackgroundColor = Colors.Black; MainGrid.Children.Add(browser); browser.NavigationStarting += async(s, args) => { if (args.Uri.OriginalString.Contains("code=")) { var code = args.Uri.Query.Replace("?code=", ""); AccessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); AccessToken = await authenticator.RefreshToken(AccessToken.RefreshToken); MainGrid.Children.Remove(browser); try { var w = await GetWeightAsync(DateTime.Now, null); Value.Text = $"{w.Weight.First().Weight}kg"; } catch (Exception ex) { // Access denied ? Debug.WriteLine(ex.Message); } } }; } catch (Exception ex) { // Something went wrong Debug.WriteLine(ex.Message); } }
//Final step. Take this authorization information and use it in the app public async Task <ActionResult> Callback() { FitbitAppCredentials appCredentials = (FitbitAppCredentials)Session["AppCredentials"]; var authenticator = new OAuth2Helper(appCredentials, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback"); string code = Request.Params["code"]; OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); //Store credentials in FitbitClient. The client in its default implementation manages the Refresh process var fitbitClient = GetFitbitClient(accessToken); ViewBag.AccessToken = accessToken; return(View()); }
public async Task <IHttpActionResult> RegistrationResult(string code) { FitbitAppCredentials appCredentials = (FitbitAppCredentials)session[AppCredentials]; var authenticator = new OAuth2Helper(appCredentials, $"{Request.RequestUri.GetLeftPart(UriPartial.Authority)}/fitbit/{nameof(RegistrationResult).ToLowerInvariant()}"); OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); // Store credentials in FitbitClient. The client in its default implementation manages the Refresh process var fitbitClient = GetFitbitClient(accessToken); // register a subscription for the authorized user // TODO: Remove hardcoded subscriber id -- should be the equivalent of our user id await fitbitClient.AddSubscriptionAsync(APICollectionType.user, "123456789"); return(Ok()); }
public async Task <HttpResponseMessage> Callback(string code) { var appCredentials = new BandAppCredentials() { ClientId = ConfigurationManager.AppSettings["BandClientId"], ClientSecret = ConfigurationManager.AppSettings["BandClientSecret"] }; var authenticator = new OAuth2Helper(appCredentials, ConfigurationManager.AppSettings["BaseUrl"] + "/api/Band/Callback"); OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); if (!string.IsNullOrEmpty(accessToken.Token)) { //Store credentials in FitbitClient. The client in its default implementation manages the Refresh process //var fitbitClient = GetBandClient(accessToken); var resp = new HttpResponseMessage(HttpStatusCode.OK); resp.Content = new StringContent(JsonConvert.SerializeObject(accessToken), Encoding.UTF8); return(new HttpResponseMessage(HttpStatusCode.OK)); } return(new HttpResponseMessage(HttpStatusCode.BadRequest)); }