示例#1
0
        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();
        }
示例#2
0
        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;
        }
示例#3
0
 public ActionResult viewUsers()
 {
     MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
     FitBitDataService FitData = new FitBitDataService(dm);
     var userCollection = FitData.GetAllFitBitUsers();
     List<FitbitUserViewModel> user_list = new List<FitbitUserViewModel>();
     foreach (var user in userCollection)
     {
         user_list.Add(new FitbitUserViewModel { user_name = user.user_name });
     }
     return View(user_list);
 }
示例#4
0
        public async Task<ActionResult> refreshDemo()
        {
            MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
            FitBitDataService FitData = new FitBitDataService(dm);
            FitBitUser user = FitData.GetFitBitUserByName("Pranay Pradhananga");

            var accessToken =  await RefreshToken(user.refresh_token, user.user_name);
            return View(accessToken);
        }
示例#5
0
 public async Task<ActionResult> getSubscriptionList()
 {
     MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
     FitBitDataService FitData = new FitBitDataService(dm);
     FitBitUser user = FitData.GetFitBitUserByName("Pranay Pradhananga");
     FitbitClient client = GetFitbitClient(user.access_token, user.refresh_token);
     string response = "";
     response = await client.GetSubscriptionString();
     return Content(response, "application/json");
 }
示例#6
0
        public async Task<ActionResult> GetHeartRateString(string user_name="Pranay Pradhananga") {
            MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
            FitBitDataService FitData = new FitBitDataService(dm);
            FitBitUser user = FitData.GetFitBitUserByName(user_name);
             string responseString = null;

            Boolean flag = true;
            while (flag)
            {
                FitbitClient client = GetFitbitClient(user.access_token, user.refresh_token);
                responseString = await client.GetHeartRateSeriesString(DateTime.Now, "7d");
                if (responseString.Length>1)
                    flag = false;
                else
                    user = await RefreshToken(user.refresh_token, user_name);
            }                 

            return Content(responseString, "application/json");
        }
示例#7
0
        public async Task<ActionResult> LastWeekStepString(string user_name="Pranay Pradhananga")
        {
            MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
            FitBitDataService FitData = new FitBitDataService(dm);
            FitBitUser user = FitData.GetFitBitUserByName(user_name);
            string response = "";
            Boolean flag = true;

            while (flag)
            {
                FitbitClient client = GetFitbitClient(user.access_token, user.refresh_token);
                response = await client.GetTimeSeriesString(TimeSeriesResourceType.Steps, DateTime.UtcNow.AddDays(-21), DateTime.UtcNow, null);
                if (response.Length > 1)
                    flag = false;
                else
                    user = await RefreshToken(user.refresh_token, user_name);
            }
            return Content(response, "application/json");

        }
示例#8
0
        public async Task<ActionResult> LastWeekSteps()
        { 
            MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
            FitBitDataService FitData = new FitBitDataService(dm);
            FitBitUser user = FitData.GetFitBitUserByName("Chris Chant");
            
            FitbitClient client = GetFitbitClient(user.access_token, user.refresh_token);

            FitbitResponse<TimeSeriesDataListInt> response = await client.GetTimeSeriesIntAsync(TimeSeriesResourceType.Steps, DateTime.UtcNow.AddDays(-7), DateTime.UtcNow);

            return View(response.Data);
        }
示例#9
0
 public async Task<ActionResult> GetUserProfile(string user_name="")
 {
     MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
     FitBitDataService FitData = new FitBitDataService(dm);
     FitBitUser user = new FitBitUser();
     FitbitResponse<UserProfile> response = null;
     Boolean flag = true;
     while (flag)
     {
         if (user_name.Length < 1)
         {
             user_name = "Pranay Pradhananga" ;
         }
         user = FitData.GetFitBitUserByName(user_name);
         FitbitClient client = GetFitbitClient(user.access_token, user.refresh_token);
         response = await client.GetUserProfileAsync();
         if (response.Data != null)
             flag = false;
         else
             user = await RefreshToken(user.refresh_token, user_name);
     }
     return View(response.Data);
 }
示例#10
0
 // Test method for updating a user information
 public async Task<int> testModifyFitbitUser() {
     MongoDataModel dm = new MongoDataModel(ConfigurationManager.AppSettings["MongoDefaultDatabase"].ToString());
     FitBitDataService FitData = new FitBitDataService(dm);
     FitBitUser user = FitData.GetFitBitUserByName("Pranay Pradhananga");
     user.expires_in = 3600;
     user.refresh_token = "d6e0f9e8d185534928067420392eb97d2a16be2ffce31f9f0379f3a7fddb125a";
     user.access_token = "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0NDc3MTQxMjMsInNjb3BlcyI6Indwcm8gd2xvYyB3aHIgd2FjdCIsInN1YiI6IjNNSzY5OSIsImF1ZCI6IjIyOVc4SiIsImlzcyI6IkZpdGJpdCIsInR5cCI6ImFjY2Vzc190b2tlbiIsImlhdCI6MTQ0NzcxMDUyM30.oiuanmhdqkBpvueI5NW_Dn0zZNr9u0gQZXSoToBlBxc";
     user.token_type = "Bearer";
     await FitData.ModifyFitBitUser(user);
     return 1;
 }