예제 #1
0
파일: FitBit.cs 프로젝트: Rumkee/Website1
        public  async Task<string> GetAccessCode(ApplicationDbContext db,Tracker t)
        {

            if (t == null)
                return "No Authorization Code";
            


            if (!string.IsNullOrEmpty(t.AccessToken) && (t.AccessTokenExpiry > DateTime.Now))
                return t.AccessToken;

            if (!string.IsNullOrEmpty(t.RefreshToken))
            {
                var Headers = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("Authorization", "Basic " + GetAuthorizationCode())
                };

                var Data = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("refresh_token", t.RefreshToken),
                    new KeyValuePair<string, string>("grant_type", "refresh_token")
                };

                var result =
                    await GenericLogic.HttpPost(Headers, Data, ConfigurationManager.AppSettings["FitbitTokenURL"]);
                dynamic jsonresponse = System.Web.Helpers.Json.Decode(result);

                string accesstoken = jsonresponse?.access_token;
                if (!string.IsNullOrEmpty(accesstoken))
                {
                    t.AccessToken = accesstoken;
                    t.RefreshToken = jsonresponse?.refresh_token;
                    t.AccessTokenExpiry = DateTime.Now.AddSeconds((jsonresponse?.expires_in*0.9));
                    t.ThirdPartyUserID = jsonresponse?.user_id;
                    db.SaveChanges();
                    return t.AccessToken;
                }

                Messaging.Add(Message.LevelEnum.alert_danger,
                    "Oops! something went wrong trying to request information from Fitbit",
                    Message.TypeEnum.TemporaryAlert, ParentTracker.User);
                db.SaveChanges();
                return string.Empty;
            }
            else
            {

                var Headers = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("Authorization", "Basic " + GetAuthorizationCode())
                };

                var Data = new List<KeyValuePair<string, string>>
                {
                    new KeyValuePair<string, string>("code", t.AuthToken),
                    new KeyValuePair<string, string>("grant_type", "authorization_code"),
                    new KeyValuePair<string, string>("client_id", ConfigurationManager.AppSettings["FitbitClientID"]),
                    new KeyValuePair<string, string>("redirect_uri",
                        ParentTracker.Url.Action("FitbitAuthCompleted", "Trackers", null,
                            ParentTracker.RequestScheme))
                };

                var result =
                    await GenericLogic.HttpPost(Headers, Data, ConfigurationManager.AppSettings["FitbitTokenURL"])
                    ;

                dynamic jsonresponse = System.Web.Helpers.Json.Decode(result);

                string accesstoken = jsonresponse?.access_token;

                if (!string.IsNullOrEmpty(accesstoken))
                {
                    t.AccessToken = accesstoken;
                    t.RefreshToken = jsonresponse?.refresh_token;
                    t.AccessTokenExpiry = DateTime.Now.AddSeconds((jsonresponse?.expires_in*0.9));
                    t.ThirdPartyUserID = jsonresponse?.user_id;
                    db.SaveChanges();
                    return t.AccessToken;
                }
                else
                {
                    //there was a problem.
                    Messaging.Add(Message.LevelEnum.alert_danger,
                        "Oops! something went wrong trying to request information from Fitbit",
                        Message.TypeEnum.TemporaryAlert, ParentTracker.User);
                    db.SaveChanges();
                    return string.Empty;
                }

            }
        }
예제 #2
0
        public Task<string> GetAccessCode(ApplicationDbContext db,Tracker t)
        {
            if (!string.IsNullOrEmpty(t?.AccessToken) && (t.AccessTokenExpiry > DateTime.Now))
                    return Task.Delay(0).ContinueWith(tsk => t.AccessToken);

            return Task.Delay(0).ContinueWith(tsk => "Runkeeper doesn't use access code");
        }
예제 #3
0
파일: FitBit.cs 프로젝트: Rumkee/Website1
        public async Task<bool> AuthenticateComplete(string userID, string code, ApplicationDbContext db)
        {

            var User = db.Users.FirstOrDefault(u => u.Id == userID);
            if (User != null && User.Id == userID && !string.IsNullOrEmpty(code))
            {

                var t = new Tracker() {AuthToken = code, Type = Tracker.TrackerType.Fitbit};
                User.Trackers.Add(t);
                await GetAccessCode(db,t);

                Messaging.Add(Message.LevelEnum.alert_success, "You have successfully linked your account to Fitbit",
                    Message.TypeEnum.StickyAlert, User);
                db.SaveChanges();
                return true;

            }


            //if we get here, something went wrong :(    
            Messaging.Add(Message.LevelEnum.alert_danger,
                "Oops! something went wrong trying to link your account to Fitbit", Message.TypeEnum.TemporaryAlert,
                User);
            db.SaveChanges();
            return false;
        }