//Refresh AccessToken when expired. public static async Task <OAuth2AccessToken> RefreshTokenAsync(string expiredToken) { HttpClient httpClient = new HttpClient(); string postUrl = OAuth2Helper.FitbitOauthPostUrl; var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "refresh_token"), new KeyValuePair <string, string>("refresh_token", expiredToken), }); string clientIdConcatSecret = OAuth2Helper.Base64Encode(ClientId + ":" + ClientSecret); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", clientIdConcatSecret); HttpResponseMessage response = await httpClient.PostAsync(postUrl, content); string responseString = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseString); OAuth2AccessToken accessToken = OAuth2Helper.ParseAccessTokenResponse(responseString); return(accessToken); }
public static async Task <OAuth2AccessToken> ExchangeAuthCodeForAccessTokenAsync(string code) { HttpClient httpClient = new HttpClient(); string postUrl = OAuth2Helper.FitbitOauthPostUrl; 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", RedirectUri) }); string clientIdConcatSecret = OAuth2Helper.Base64Encode(ClientId + ":" + ClientSecret); httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", clientIdConcatSecret); HttpResponseMessage response = await httpClient.PostAsync(postUrl, content); string responseString = await response.Content.ReadAsStringAsync(); Console.WriteLine(responseString); OAuth2AccessToken accessToken = OAuth2Helper.ParseAccessTokenResponse(responseString); return(accessToken); }
public void OAuth2Helper_Can_Deserialize_AccessToken() { string content = SampleDataHelper.GetContent("AccessToken.json"); var result = OAuth2Helper.ParseAccessTokenResponse(content); Validate(result); }
public void OAuth2Helper_Throws_FitbitTokenException() { string content = SampleDataHelper.GetContent("ApiError-Request-StaleToken.json"); try { OAuth2Helper.ParseAccessTokenResponse(content); } catch (FitbitException exception) { // can only use ShouldThrow on async funcs of Task exception.ApiErrors.Count.Should().Be(1); } }