public async Task <Activity> GetDayActivityAsync(string Currentdate, string userId)
        {
            var appCredentials = AppCredentials.ToString();


            var oAuth_params = OAuthUtility.BuildBasicParameters(AppCredentials.ConsumerKey, AppCredentials.ConsumerSecret, "https://wbsapi.withings.net", HttpMethod.Get, this.AccessToken)
                               .Where(p => p.Key != "oauth_signature")
                               .OrderBy(p => p.Key);

            string startdate = ("2017-02-30");
            string date      = ("2017-03-24");

            string requestUri = $"https://wbsapi.withings.net/v2/measure?action=getactivity&userid={userId}&date={date}&";

            requestUri += string.Join("&", oAuth_params.Select(kvp => kvp.Key + "=" + kvp.Value));

            var signature = OAuthUtility.BuildBasicParameters(AppCredentials.ConsumerKey, AppCredentials.ConsumerSecret, requestUri, HttpMethod.Get, this.AccessToken)
                            .First((KeyValuePair <string, string> p) => p.Key == "oauth_signature").Value;

            string json = await HttpClient.GetStringAsync(requestUri + "&oauth_signature=" + signature);

            var o = JObject.Parse(json);


            return(new Activity
            {
                Calories = (float)o["body"]["calories"],
                Date = (string)o["body"]["date"],
                Distance = (float)o["body"]["distance"],
                Elevation = (float)o["body"]["elevation"],
                Intense = (int)o["body"]["intense"],
                Moderate = (int)o["body"]["moderate"],
                Soft = (int)o["body"]["soft"],
                Steps = (int)o["body"]["steps"],
                TimeZone = (string)o["body"]["timezone"],
                TotalCalories = (float)o["body"]["totalcalories"]
            });
        }
        public async Task <IEnumerable <MeasureGroup> > GetBodyMeasureAsync(string userId, string deviceType)
        {
            var appCredentials = AppCredentials.ToString();


            var oAuth_params = OAuthUtility.BuildBasicParameters(AppCredentials.ConsumerKey, AppCredentials.ConsumerSecret, "https://wbsapi.withings.net", HttpMethod.Get, this.AccessToken)
                               .Where(p => p.Key != "oauth_signature")
                               .OrderBy(p => p.Key);


            string requestUri = $"https://wbsapi.withings.net/measure?action=getmeas&userid={userId}&devtype={deviceType}&";

            requestUri += string.Join("&", oAuth_params.Select(kvp => kvp.Key + "=" + kvp.Value));

            var signature = OAuthUtility.BuildBasicParameters(AppCredentials.ConsumerKey, AppCredentials.ConsumerSecret, requestUri, HttpMethod.Get, this.AccessToken)
                            .First((KeyValuePair <string, string> p) => p.Key == "oauth_signature").Value;

            string json = await HttpClient.GetStringAsync(requestUri + "&oauth_signature=" + signature);

            var o = JObject.Parse(json);

            return(JsonConvert.DeserializeObject <IEnumerable <MeasureGroup> >(o["body"]["measuregrps"].ToString()));
        }
Exemple #3
0
        public async Task <ActionResult> AccessTokenFlow()
        {
            //seting the authorizer varable with consumerKey/Secret as in Withings. Will become variable for later injection
            //grab value in URL to place in varables

            var authorizer = new OAuthAuthorizer(consumerKey, consumerSecret);

            var accessToken   = Request.QueryString["oauth_token"].ToString();
            var oAuthVerifier = Request.QueryString["oauth_verifier"].ToString();
            List <KeyValuePair <string, string> > parameters = new List <KeyValuePair <string, string> >();

            parameters.Add(new KeyValuePair <string, string>("oauth_token", accessToken));



            //  List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
            //   parameters.Add(new KeyValuePair<string, string>("oauth_verifier", oAuthVerifier));

            var requestToken = Session["requestToken"] as RequestToken;

            //send them out as access_tokens to get access granted by Withings
            var accessTokenResponse = await authorizer.GetAccessToken("https://oauth.withings.com/account/access_token", requestToken, oAuthVerifier);

            var accessTokens = accessTokenResponse.Token;

            string userId = Request.QueryString["userid"]; //todo: Find out how to assign the real user id from OAuth call



            var client = OAuthUtility.CreateOAuthClient(consumerKey, consumerSecret, accessTokens);


            //string withingsDateApiUrl = "&date=";

            //string withingsStartDateApiUrl = "&startdateymd=";

            //string withingsEndDateApiUrl = "&enddateymd=";
            //DateTime date = DateTime.Now;
            //string dateFormat = date.ToString("yyyy-MM-dd");
            //string startDateFormat = "2017-03-10";

            //string endDateFormat = "2017-03-21";

            // string dateFormat = "2017-03-13";

            //string oauthenticator = "&"+consumerSecret+"&"+accessToken;
            var oAuth_params = OAuthUtility.BuildBasicParameters(consumerKey, consumerSecret, "https://wbsapi.withings.net", HttpMethod.Get, accessTokens)
                               .Where(p => p.Key != "oauth_signature")
                               .OrderBy(p => p.Key);


            string requestUri = $"https://wbsapi.withings.net/measure?action=getmeas&userid={userId}&";

            requestUri += string.Join("&", oAuth_params.Select(kvp => kvp.Key + "=" + kvp.Value));

            var signature = OAuthUtility.BuildBasicParameters(consumerKey, consumerSecret, requestUri, HttpMethod.Get, accessTokens)
                            .First(p => p.Key == "oauth_signature").Value;

            string json = await client.GetStringAsync(requestUri + "&oauth_signature=" + signature);

            var o = JObject.Parse(json);

            int updateTime = (int)o["body"]["updatetime"];

            ViewBag.measureGroups = o["body"]["measuregrps"].Select(no => new
            {
                GroupId = no["grpid"],
                Attrib  = no["attrib"]
            });

            ViewBag.serializedResult = "JsonData";
            return(View("AccessTokenFlow"));
        }