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(); }
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); }
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; }