예제 #1
0
        public async Task <ExpandoObject> GetWorkouts(string startday, string endday, string token, string secret)
        {
            var query = BaseUri.AppendPathSegment("measure").SetQueryParam("action", "getworkouts")
                        .SetQueryParam("startdateymd", startday).SetQueryParam("enddateymd", endday);
            var    oAuth     = new OAuthBase();
            var    nonce     = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var    timeStamp = oAuth.GenerateTimeStamp();
            string normalizedUrl;
            string parameters;
            var    signature = oAuth.GenerateSignature(new Uri(query), _credentials.ConsumerKey, _credentials.ConsumerSecret,
                                                       token, secret, "GET", timeStamp, nonce,
                                                       OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out parameters);

            query.SetQueryParam("oauth_consumer_key", _credentials.ConsumerKey);
            query.SetQueryParam("oauth_nonce", nonce);
            query.SetQueryParam("oauth_signature", signature);
            query.SetQueryParam("oauth_signature_method", "HMAC-SHA1");
            query.SetQueryParam("oauth_timestamp", timeStamp);
            query.SetQueryParam("oauth_token", token);
            query.SetQueryParam("oauth_version", "1.0");

            return(await query.GetJsonAsync().ConfigureAwait(false));
        }
        public WithingsBody GetBodyMeasures(string userid, DateTime lastupdate, string token, string secret)
        {
            var    oAuth     = new OAuthBase();
            var    nonce     = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var    timeStamp = oAuth.GenerateTimeStamp();
            var    uri       = $"https://wbsapi.withings.net/measure?action=getmeas&userid=29&lastupdate={lastupdate.ToUnixTime()}";
            string normalizedUrl;
            string parameters;
            var    signature = oAuth.GenerateSignature(new Uri(uri), _credentials.ConsumerKey, _credentials.ConsumerSecret,
                                                       token, secret, "GET", timeStamp, nonce,
                                                       OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out parameters);
            var request = new RestRequest("measure", Method.GET);

            request
            .AddQueryParameter("action", "getactivity")
            .AddQueryParameter("userid", userid)
            .AddQueryParameter("lastupdate", lastupdate.ToUnixTime().ToString());
            AddOAuthParameters(request, nonce, timeStamp, signature, token);

            IRestResponse <WithingsBody> response = Client.Execute <WithingsBody>(request);

            return(response.Data);
        }
        public string GetSleepSummary(string startday, string endday, string token, string secret)
        {
            var oAuth = new OAuthBase();

            var    nonce     = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var    timeStamp = oAuth.GenerateTimeStamp();
            var    uri       = $"{BaseUri}/sleep?action=getsummary&startdateymd={startday}&enddateymd={endday}";
            string normalizedUrl;
            string parameters;
            var    signature = oAuth.GenerateSignature(new Uri(uri), _credentials.ConsumerKey, _credentials.ConsumerSecret,
                                                       token, secret, "GET", timeStamp, nonce,
                                                       OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out parameters);

            signature = HttpUtility.UrlEncode(signature);

            var requestUri = new StringBuilder(uri);

            requestUri.AppendFormat("&oauth_consumer_key={0}&", _credentials.ConsumerKey);
            requestUri.AppendFormat("oauth_nonce={0}&", nonce);
            requestUri.AppendFormat("oauth_signature={0}&", signature);
            requestUri.AppendFormat("oauth_signature_method={0}&", "HMAC-SHA1");
            requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
            requestUri.AppendFormat("oauth_token={0}&", token);
            requestUri.AppendFormat("oauth_version={0}", "1.0");

            var          wrGeturl  = WebRequest.Create(requestUri.ToString());
            var          objStream = wrGeturl.GetResponse().GetResponseStream();
            const string sLine     = "";

            if (objStream == null)
            {
                return(sLine);
            }
            var objReader = new StreamReader(objStream);

            return(objReader.ReadLine());
        }
        public WithingsWeighInResponse GetActivityMeasures(DateTime startDay, DateTime endDay, string userId, string token, string secret)
        {
            var    oAuth     = new OAuthBase();
            var    request   = new RestRequest("measure", Method.GET);
            var    nonce     = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var    timeStamp = oAuth.GenerateTimeStamp();
            var    uri       = $"{BaseUri}/measure?action=getactivity&userid={userId}&startdateymd={startDay:yyyy-MM-dd}&enddateymd={endDay:yyyy-MM-dd}";
            string normalizedUrl;
            string parameters;
            var    signature = oAuth.GenerateSignature(new Uri(uri), _credentials.ConsumerKey, _credentials.ConsumerSecret,
                                                       token, secret, "GET", timeStamp, nonce,
                                                       OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out parameters);

            request
            .AddQueryParameter("action", "getactivity")
            .AddQueryParameter("userid", userId)
            .AddQueryParameter("startdateymd", startDay.ToString("yyyy-MM-dd"))
            .AddQueryParameter("enddateymd", startDay.ToString("yyyy-MM-dd"));
            AddOAuthParameters(request, nonce, timeStamp, signature, token);

            IRestResponse <WithingsWeighInResponse> response = Client.Execute <WithingsWeighInResponse>(request);

            return(response.Data);
        }