Exemplo n.º 1
0
        public async Task<OAuth2AccessToken> ExchangeAuthCodeForAccessTokenAsync(string code)
        {
            HttpClient httpClient = new HttpClient();

            string postUrl = FitbitApiBaseUrl;
            postUrl += OAuthBase;
            postUrl += "/token";

            var content = new FormUrlEncodedContent(new[] 
            {
                new KeyValuePair<string, string>("grant_type", "authorization_code"),
                new KeyValuePair<string, string>("client_id", ClientId),
                //new KeyValuePair<string, string>("client_secret", AppSecret),
                new KeyValuePair<string, string>("code", code),
                new KeyValuePair<string, string>("redirect_uri", this.RedirectUri)
            });


            string clientIdConcatSecret = Base64Encode(ClientId + ":" + AppSecret);

            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", clientIdConcatSecret); 

            HttpResponseMessage response = await httpClient.PostAsync(postUrl, content);
            string responseString = await response.Content.ReadAsStringAsync();

            JObject responseObject = JObject.Parse(responseString);

            // Note: if user cancels the auth process Jawbone returns a 200 response, but the JSON payload is way different.
            var error = responseObject["error"];
            if (error != null)
            {
                // TODO: Actually should probably raise an exception here maybe?
                return null;
            }

            OAuth2AccessToken accessToken = new OAuth2AccessToken();

            var temp_access_token = responseObject["access_token"];
            if (temp_access_token != null) accessToken.Token = temp_access_token.ToString();

            var temp_expires_in = responseObject["expires_in"];
            if (temp_expires_in != null) accessToken.ExpiresIn = Convert.ToInt32(temp_expires_in.ToString());

            var temp_token_type = responseObject["token_type"];
            if (temp_token_type != null) accessToken.TokenType = temp_token_type.ToString();

            var temp_refresh_token = responseObject["refresh_token"];
            if (temp_refresh_token != null) accessToken.RefreshToken = temp_refresh_token.ToString();

            return accessToken;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Simplest constructor for OAuth2- requires the minimum information required by FitBit.Net client to make succesful calls to Fitbit Api
        /// </summary>
        /// <param name="credentials">Obtain this information from your developer dashboard. App credentials are required to perform token refresh</param>
        /// <param name="accessToken">Authenticate with Fitbit API using OAuth2. Authenticator2 class is a helper for this process</param>
        /// <param name="interceptor">An interface that enables sniffing all outgoing and incoming http requests from FitbitClient</param>
        public FitbitClient(FitbitAppCredentials credentials, OAuth2AccessToken accessToken, List <IFitbitInterceptor> interceptors, bool enableOAuth2TokenRefresh = true, ITokenManager tokenManager = null)
        {
            this.AppCredentials = credentials;
            this.AccessToken    = accessToken;

            this.FitbitInterceptorPipeline = new List <IFitbitInterceptor>();

            if (interceptors != null && interceptors.Count > 0)
            {
                this.FitbitInterceptorPipeline.AddRange(interceptors);
            }

            ConfigureTokenManager(tokenManager);

            //Auto refresh should always be the last handle to be registered.
            ConfigureAutoRefresh(enableOAuth2TokenRefresh);
            CreateHttpClientForOAuth2();
        }
Exemplo n.º 3
0
        private async Task<FitBitUser> getNewUser(OAuth2AccessToken token) {

            //get the user name by using the access token being currently recieved
            FitbitClient client = GetFitbitClient(token.Token, token.RefreshToken);
            FitbitResponse<UserProfile> response = await client.GetUserProfileAsync();

            // Create a new user with the access token recieved and user name 
            FitBitUser user = new FitBitUser();
            user.user_name = response.Data.FullName;
            user.access_token = token.Token;
            user.token_type = token.TokenType;
            user.expires_in = token.ExpiresIn;
            user.refresh_token = token.RefreshToken;

            return user;
        }
Exemplo n.º 4
0
        public async Task <OAuth2AccessToken> ExchangeAuthCodeForAccessTokenAsync(string code)
        {
            HttpClient httpClient = new HttpClient();

            string postUrl = FitbitApiBaseUrl;

            postUrl += OAuthBase;
            postUrl += "/token";

            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("grant_type", "authorization_code"),
                new KeyValuePair <string, string>("client_id", ClientId),
                //new KeyValuePair<string, string>("client_secret", AppSecret),
                new KeyValuePair <string, string>("code", code),
                new KeyValuePair <string, string>("redirect_uri", this.RedirectUri)
            });


            string clientIdConcatSecret = Base64Encode(ClientId + ":" + AppSecret);

            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", clientIdConcatSecret);

            HttpResponseMessage response = await httpClient.PostAsync(postUrl, content);

            string responseString = await response.Content.ReadAsStringAsync();

            JObject responseObject = JObject.Parse(responseString);

            // Note: if user cancels the auth process Jawbone returns a 200 response, but the JSON payload is way different.
            var error = responseObject["error"];

            if (error != null)
            {
                // TODO: Actually should probably raise an exception here maybe?
                return(null);
            }

            OAuth2AccessToken accessToken = new OAuth2AccessToken();

            var temp_access_token = responseObject["access_token"];

            if (temp_access_token != null)
            {
                accessToken.Token = temp_access_token.ToString();
            }

            var temp_expires_in = responseObject["expires_in"];

            if (temp_expires_in != null)
            {
                accessToken.ExpiresIn = Convert.ToInt32(temp_expires_in.ToString());
            }

            var temp_token_type = responseObject["token_type"];

            if (temp_token_type != null)
            {
                accessToken.TokenType = temp_token_type.ToString();
            }

            var temp_refresh_token = responseObject["refresh_token"];

            if (temp_refresh_token != null)
            {
                accessToken.RefreshToken = temp_refresh_token.ToString();
            }

            return(accessToken);
        }
Exemplo n.º 5
0
 public FitbitClient(FitbitAppCredentials credentials, OAuth2AccessToken accessToken, IFitbitInterceptor interceptor, ITokenManager tokenManager) : this(credentials, accessToken, interceptor, true, tokenManager)
 {
 }
Exemplo n.º 6
0
 public FitbitClient(FitbitAppCredentials credentials, OAuth2AccessToken accessToken, List <IFitbitInterceptor> interceptors, bool enableOAuth2TokenRefresh) : this(credentials, accessToken, interceptors, enableOAuth2TokenRefresh, null)
 {
 }
Exemplo n.º 7
0
 public FitbitClient(FitbitAppCredentials credentials, OAuth2AccessToken accessToken, bool enableOAuth2TokenRefresh) : this(credentials, accessToken, null, enableOAuth2TokenRefresh)
 {
 }
Exemplo n.º 8
0
        public async Task<OAuth2AccessToken> RefreshAccessTokenAsync(string refresh_token) {
            HttpClient httpClient = new HttpClient();

            string postUrl = FitbitApiBaseUrl;
            postUrl += OAuthBase;
            postUrl += "/token";

            var content = new FormUrlEncodedContent(new[] {
                    new KeyValuePair<string, string>("grant_type", "refresh_token"),
                    new KeyValuePair<string, string>("refresh_token", refresh_token)
            });

            string clientIdConcatSecret = Base64Encode(ClientId + ":" + AppSecret);
            httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", clientIdConcatSecret);

            HttpResponseMessage response = await httpClient.PostAsync(postUrl, content);
            string responseString = await response.Content.ReadAsStringAsync();

            JObject responseObject = JObject.Parse(responseString);
            var error = responseObject["error"];
            if (error != null)
            {
                //TODO: Raise exception and handle the exception
                return null;
            }

            OAuth2AccessToken accessToken = new OAuth2AccessToken();
            var temp_access_token = responseObject["access_token"];
            if (temp_access_token != null) accessToken.Token = temp_access_token.ToString();

            var temp_expires_in = responseObject["expires_in"];
            if (temp_expires_in != null) accessToken.ExpiresIn = Convert.ToInt32(temp_expires_in.ToString());

            var temp_refresh_token = responseObject["refresh_token"];
            if (temp_refresh_token != null) accessToken.RefreshToken = temp_refresh_token.ToString();

            var temp_token_type = responseObject["token_type"];
            if (temp_token_type != null) accessToken.TokenType = temp_token_type.ToString();


            return accessToken;
        }