예제 #1
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();
                }
            }
        }
예제 #2
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();
                }
            }
        }