Ejemplo n.º 1
0
        public static void RevokeAccessToken(string tokenToBeRevoked)
        {
            TokenRevoked?.Invoke();

            WebClient client      = new WebClient();
            string    accessToken = SecretStorage.GetFitbitClientID() + ":" + SecretStorage.GetFitbitClientSecret();

            accessToken = Base64Encode(accessToken);
            client.Headers.Add("Authorization", "Basic " + accessToken);

            var values = new NameValueCollection();

            values["token"] = tokenToBeRevoked;

            try
            {
                var response       = client.UploadValues(REVOKE_URL, values);
                var responseString = Encoding.Default.GetString(response);

                SecretStorage.RemoveAccessToken(tokenToBeRevoked);
                SecretStorage.RemoveRefreshToken(SecretStorage.GetRefreshToken());
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
                Logger.WriteToConsole(e.ToString());
            }
            finally
            {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        internal static void RefreshAccessToken()
        {
            Logger.WriteToConsole("Access token not valid anymore. Try to refresh access token.");

            WebClient client      = new WebClient();
            string    accessToken = SecretStorage.GetFitbitClientID() + ":" + SecretStorage.GetFitbitClientSecret();

            accessToken = Base64Encode(accessToken);
            client.Headers.Add("Authorization", "Basic " + accessToken);

            var values = new NameValueCollection();

            values["grant_type"] = "refresh_token";
            string refreshToken = SecretStorage.GetRefreshToken();

            values["refresh_token"] = refreshToken;
            values["expires_in"]    = "" + Settings.TOKEN_LIFETIME;

            try
            {
                var response       = client.UploadValues(REFRESH_URL, values);
                var responseString = Encoding.Default.GetString(response);
                AccessRefreshResponse accessResponse = JsonConvert.DeserializeObject <AccessRefreshResponse>(responseString);
                Logger.WriteToConsole("Refreshing token returned the following response: " + responseString);
                Logger.WriteToConsole("Writing access and refresh token to database.");

                Database.GetInstance().LogInfo("Retreived new access and refresh token: " + accessResponse.access_token + " / " + accessResponse.refresh_token);
                SecretStorage.SaveAccessToken(accessResponse.access_token);
                SecretStorage.SaveRefreshToken(accessResponse.refresh_token);
            }
            catch (WebException e)
            {
                if ((e.Response is HttpWebResponse) && ((e.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized || (e.Response as HttpWebResponse).StatusCode == HttpStatusCode.BadRequest))
                {
                    RefreshTokenFail?.Invoke();
                }
                else if ((e.Response is HttpWebResponse) && (e.Response as HttpWebResponse).StatusCode.ToString().Equals("429"))
                {
                    Logger.WriteToConsole("Too many requests");
                }
                else
                {
                    Logger.WriteToLogFile(e);
                }
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
                Logger.WriteToConsole(e.ToString());
            }
            finally
            {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
Ejemplo n.º 3
0
        //Generic method that retrieves specific data from the fitbit. If an exception is thrown during this process, it checks whether the problem is an authorization problem. In this case, the tokens are refreshed.
        //The method returns a tuple, consisting of two values. The first item in the tuple is the retrieved data set, or the default value in case an exception was thrown and the second item, indicates whether a caller
        //of this method should retry to call this method in case of an exception.
        private static Tuple <T, bool> GetDataFromFitbit <T>(string url)
        {
            WebClient    client = null;
            Stream       data   = null;
            StreamReader reader = null;

            try
            {
                client = new WebClient();
                client.Headers.Add("Authorization", "Bearer " + SecretStorage.GetAccessToken());

                data = client.OpenRead(url);

                reader = new StreamReader(data);
                string response = reader.ReadToEnd();

                T dataObject = JsonConvert.DeserializeObject <T>(response);
                return(Tuple.Create <T, bool>(dataObject, false));
            }
            catch (WebException e)
            {
                if ((e.Response is HttpWebResponse) && (e.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
                {
                    RefreshAccessToken();
                    return(Tuple.Create <T, bool>(default(T), true));
                }
                else if ((e.Response is HttpWebResponse) && (e.Response as HttpWebResponse).StatusCode.ToString().Equals("429"))
                {
                    Logger.WriteToConsole("Too many requests");
                    return(Tuple.Create <T, bool>(default(T), false));
                }
                else
                {
                    Logger.WriteToLogFile(e);
                    return(Tuple.Create <T, bool>(default(T), false));
                }
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
                return(Tuple.Create <T, bool>(default(T), false));
            }
            finally
            {
                if (data != null)
                {
                    data.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }
Ejemplo n.º 4
0
        internal static void GetFirstAccessToken(string registrationToken)
        {
            WebClient client = new WebClient();

            try
            {
                Logger.WriteToConsole("Try to get first access token");

                client.Headers.Add("Authorization", "Basic " + SecretStorage.GetFibitFirstAuthorizationCode());

                var values = new NameValueCollection();
                values["clientId"]     = SecretStorage.GetFitbitClientID();
                values["grant_type"]   = "authorization_code";
                values["redirect_uri"] = Settings.REDIRECT_URI;
                values["code"]         = registrationToken;

                var response       = client.UploadValues(REFRESH_URL, values);
                var responseString = Encoding.Default.GetString(response);
                AccessRefreshResponse accessResponse = JsonConvert.DeserializeObject <AccessRefreshResponse>(responseString);

                Database.GetInstance().LogInfo("Retreived new access and refresh token: " + accessResponse.access_token + " / " + accessResponse.refresh_token);
                SecretStorage.SaveAccessToken(accessResponse.access_token);
                SecretStorage.SaveRefreshToken(accessResponse.refresh_token);

                client.Dispose();
            }
            catch (Exception e)
            {
                Logger.WriteToLogFile(e);
            }
            finally
            {
                if (client != null)
                {
                    client.Dispose();
                }
            }
        }