private async Task <OAuthAccessTokenResponse> RetrieveAuthToken() { OAuthAccessTokenResponse authToken = null; Uri uri = new Uri(new Uri(_baseUrl), Constants.DsApiEndPoints.OAuthTokenUrl); MultipartFormDataContent httpContent = new MultipartFormDataContent { { new StringContent(Constants.DsOauthConstants.ValueGrantType), "\"" + Constants.DsOauthConstants.KeyGrantType + "\"" }, { new StringContent(Constants.DsOauthConstants.ValueClientId), "\"" + Constants.DsOauthConstants.KeyClientId + "\"" }, { new StringContent(Constants.DsOauthConstants.ValueClientSecret), "\"" + Constants.DsOauthConstants.KeyClientSecret + "\"" }, { new StringContent(Constants.DsOauthConstants.ValueUsername), "\"" + Constants.DsOauthConstants.KeyUsername + "\"" }, { new StringContent(Constants.DsOauthConstants.ValuePassword), "\"" + Constants.DsOauthConstants.KeyPassword + "\"" }, }; _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Constants.DsOauthConstants.AuthorizationToken); HttpResponseMessage response = await _httpClient.PostAsync(uri, httpContent); if (response.IsSuccessStatusCode) { string responseStr = await response.Content.ReadAsStringAsync(); authToken = JsonConvert.DeserializeObject <OAuthAccessTokenResponse>(responseStr); } return(authToken); }
private async Task <HttpClient> GetHttpClient() { if (_httpClient == null) { _httpClient = new HttpClient(); } if (_tokenResponse == null) { OAuthAccessTokenResponse authToken = await RetrieveAuthToken(); if (authToken == null) { //return null; authToken = new OAuthAccessTokenResponse() { AccessToken = "dummy-access-token" }; } else { _tokenResponse = authToken; } } //_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _tokenResponse.AccessToken); return(_httpClient); }
/// <summary> /// Following the 3-legged authorization, you can exchange a request token for an access token /// using this method. This is the third and final step of the authorization process. /// </summary> /// <param name="verifier">The verification key received after the user has accepted the app.</param> /// <returns>An instance of <see cref="OAuthAccessTokenResponse"/> representing the response.</returns> public override OAuthAccessTokenResponse GetAccessToken(string verifier) { // Flickr apparently deviates from the OAuth 1.0a specification, since they require the OAuth properties to // be specified in the query string. // Some error checking if (AccessTokenUrl == null) { throw new PropertyNotSetException("AccessTokenUrl"); } if (ConsumerKey == null) { throw new PropertyNotSetException("ConsumerKey"); } if (ConsumerSecret == null) { throw new PropertyNotSetException("ConsumerSecret"); } if (Token == null) { throw new PropertyNotSetException("Token"); } // Initialize the query string HttpQueryString queryString = new HttpQueryString { { "oauth_verifier", verifier } }; // Generate the OAuth signature string signature = GenerateSignature(HttpMethod.Get, AccessTokenUrl, queryString, null); // Append the OAuth properties to the query string queryString.Add("oauth_nonce", Nonce); queryString.Add("oauth_timestamp", Timestamp); queryString.Add("oauth_consumer_key", ConsumerKey); queryString.Add("oauth_signature_method", "HMAC-SHA1"); queryString.Add("oauth_version", "1.0"); queryString.Add("oauth_token", Token); if (!string.IsNullOrWhiteSpace(Callback)) { queryString.Add("oauth_callback", Callback); } queryString.Add("oauth_signature", signature); // Make the call to the API IHttpResponse response = HttpUtils.Requests.Get(AccessTokenUrl, queryString); // Validate the response FlickrResponse.ValidateResponse(response); // Parse the response body OAuthAccessToken body = OAuthAccessToken.Parse(this, response.Body); // Parse the response return(OAuthAccessTokenResponse.ParseResponse(response, body)); }
public override OAuthAccessTokenResponse GetAccessToken(string verifier) { // Make the call to the API/provider IHttpResponse response = GetAccessTokenResponse(verifier); // Parse the response body OAuthAccessToken body = TwitterOAuthAccessToken.Parse(this, response.Body); // Parse the response return(OAuthAccessTokenResponse.ParseResponse(response, body)); }
/// <summary> /// Following the 3-legged authorization, you can exchange a request token for an access token /// using this method. This is the third and final step of the authorization process. /// </summary> /// <param name="verifier">The verification key received after the user has accepted the app.</param> /// <returns>Returns an instance of <see cref="OAuthAccessTokenResponse"/> representing the response.</returns> /// <see> /// <cref>https://dev.twitter.com/docs/auth/3-legged-authorization</cref> /// </see> public virtual OAuthAccessTokenResponse GetAccessToken(string verifier) { // Make the call to the API/provider SocialHttpResponse response = GetAccessTokenResponse(verifier); // Parse the response body OAuthAccessToken body = OAuthAccessToken.Parse(this, response.Body); // Parse the response return(OAuthAccessTokenResponse.ParseResponse(response, body)); }
/// <summary> /// Following the 3-legged authorization, you can exchange a request token for an access token /// using this method. This is the third and final step of the authorization process. /// </summary> /// <param name="verifier">The verification key received after the user has accepted the app.</param> /// <returns>An instance of <see cref="OAuthAccessTokenResponse"/> representing the response.</returns> /// <see> /// <cref>https://dev.twitter.com/docs/auth/3-legged-authorization</cref> /// </see> public virtual OAuthAccessTokenResponse GetAccessToken(string verifier) { // Some error checking if (string.IsNullOrWhiteSpace(verifier)) { throw new ArgumentNullException(nameof(verifier)); } // Make the call to the API/provider IHttpResponse response = GetAccessTokenResponse(verifier); // Parse the response body OAuthAccessToken body = OAuthAccessToken.Parse(this, response.Body); // Parse the response return(OAuthAccessTokenResponse.ParseResponse(response, body)); }
private async Task <Response <T> > ResolveResponse <T>(HttpRequest request) { Response <T> result; HttpClient httpClient = await GetHttpClient(); //check if retrieving token was successfull if (httpClient == null) { return(new Response <T>(Enum.ResponseError.AuthorizationError)); } UriBuilder uriBuilder = new UriBuilder(request.Uri) { Port = -1 }; NameValueCollection query = HttpUtility.ParseQueryString(uriBuilder.Query); query.Add(request.Parameters); uriBuilder.Query = query.ToString(); HttpResponseMessage response; if (request.HttpRequestType == HttpRequestType.GET) { response = await httpClient.GetAsync(uriBuilder.Uri); } else { response = await httpClient.PostAsync(uriBuilder.Uri, request.HttpContent); } if (response.IsSuccessStatusCode) { string content = await response.Content.ReadAsStringAsync(); try { T saveContactKeyResponse = JsonConvert.DeserializeObject <T>(content); result = new Response <T>(saveContactKeyResponse); } catch (JsonReaderException e) { result = new Response <T>(Enum.ResponseError.ContentError, e); } } else { if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized) { OAuthAccessTokenResponse authToken = await RetrieveAuthToken(); if (authToken == null) { result = new Response <T>(Enum.ResponseError.AuthorizationError); } else { _tokenResponse = authToken; if (request.retryCount < Constants.AuthTokenRetryLimit) { request.retryCount++; result = await ResolveResponse <T>(request); } else { result = new Response <T>(Enum.ResponseError.AuthorizationError); } } } else { result = new Response <T>(Enum.ResponseError.ResponseError); } } return(result); }