public async Task<ActionResult> Callback() { // FitBit data model to connect Mongo database // Database settings stored in Webconfig--> appSettings MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString()); FitBitDataService FitData = new FitBitDataService(dm); FitbitContext fitContext = new FitbitContext(); Authenticator2 authenticator = new Authenticator2(fitContext.ClientId, fitContext.ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback"); //get authorisation code and exchange for access token string code = Request.Params["code"]; OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); //get the user name by using the access token being currently recieved FitbitClient client = GetFitbitClient(accessToken.Token, accessToken.RefreshToken); FitbitResponse<UserProfile> response = await client.GetUserProfileAsync(); FitBitUser new_user = await getNewUser(accessToken); //add the new user to the database await FitData.NewFitBitUser(new_user); Session["AccessToken"] = accessToken; System.Diagnostics.Debug.WriteLine("Access Token is: {0} and Expires in: {1} ", accessToken.Token, accessToken.ExpiresIn); return View(); }
//Final step. Take this authorization information and use it in the app public async Task<ActionResult> Callback() { //make sure you've set these up in Web.Config under <appSettings>: string ConsumerKey = ConfigurationManager.AppSettings["FitbitConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["FitbitConsumerSecret"]; string ClientId = ConfigurationManager.AppSettings["FitbitClientId"]; Fitbit.Api.Portable.Authenticator2 authenticator = new Fitbit.Api.Portable.Authenticator2(ClientId, ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback" ); string code = Request.Params["code"]; OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); // For demo, put this in the session managed by ASP.NET Session["AccessToken"] = accessToken; /* Session["FitbitAuthToken"] = credential.AuthToken; Session["FitbitAuthTokenSecret"] = credential.AuthTokenSecret; Session["FitbitUserId"] = credential.UserId; */ return RedirectToAction("Index", "Home"); }
//Final step. Take this authorization information and use it in the app public async Task <ActionResult> Callback() { //make sure you've set these up in Web.Config under <appSettings>: string ConsumerKey = ConfigurationManager.AppSettings["FitbitConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["FitbitConsumerSecret"]; string ClientId = ConfigurationManager.AppSettings["FitbitClientId"]; Fitbit.Api.Portable.Authenticator2 authenticator = new Fitbit.Api.Portable.Authenticator2(ClientId, ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback" ); string code = Request.Params["code"]; OAuth2AccessToken accessToken = await authenticator.ExchangeAuthCodeForAccessTokenAsync(code); // For demo, put this in the session managed by ASP.NET Session["AccessToken"] = accessToken; /* * * Session["FitbitAuthToken"] = credential.AuthToken; * Session["FitbitAuthTokenSecret"] = credential.AuthTokenSecret; * Session["FitbitUserId"] = credential.UserId; * */ return(RedirectToAction("Index", "Home")); }
public ActionResult Authorize() { FitbitContext fitContext = new FitbitContext(); Authenticator2 authenticator = new Authenticator2(fitContext.ClientId, fitContext.ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback"); string[] scopes = new string[] { "profile", "activity", "heartrate", "location" }; string authUrl = authenticator.GenerateAuthUrl(scopes, null); return Redirect(authUrl); }
// // GET: /FitbitAuth/ // Setup - prepare the user redirect to Fitbit.com to prompt them to authorize this app. public ActionResult Authorize() { //make sure you've set these up in Web.Config under <appSettings>: string ConsumerKey = ConfigurationManager.AppSettings["FitbitConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["FitbitConsumerSecret"]; string ClientId = ConfigurationManager.AppSettings["FitbitClientId"]; Fitbit.Api.Portable.Authenticator2 authenticator = new Fitbit.Api.Portable.Authenticator2(ClientId, ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback" ); string[] scopes = new string[] { "profile" }; string authUrl = authenticator.GenerateAuthUrl(scopes, null); return(Redirect(authUrl)); }
// // GET: /FitbitAuth/ // Setup - prepare the user redirect to Fitbit.com to prompt them to authorize this app. public ActionResult Authorize() { //make sure you've set these up in Web.Config under <appSettings>: string ConsumerKey = ConfigurationManager.AppSettings["FitbitConsumerKey"]; string ConsumerSecret = ConfigurationManager.AppSettings["FitbitConsumerSecret"]; string ClientId = ConfigurationManager.AppSettings["FitbitClientId"]; Fitbit.Api.Portable.Authenticator2 authenticator = new Fitbit.Api.Portable.Authenticator2(ClientId, ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback" ); string[] scopes = new string[] {"profile"}; string authUrl = authenticator.GenerateAuthUrl(scopes, null); return Redirect(authUrl); }
private async Task<FitBitUser> RefreshToken(string refresh_token, string user_name) { MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString()); FitBitDataService FitData = new FitBitDataService(dm); FitbitContext fitContext = new FitbitContext(); Authenticator2 authenticator = new Authenticator2(fitContext.ClientId, fitContext.ConsumerSecret, Request.Url.GetLeftPart(UriPartial.Authority) + "/Fitbit/Callback"); OAuth2AccessToken access_token = await authenticator.RefreshAccessTokenAsync(refresh_token); //TODO: update the database with the new token FitBitUser user = await getNewUser(access_token); //update the user token await FitData.ModifyFitBitUser(user); return user; }