Getting 409 conflict error in OAuth 1 when using an API in C#

59 views Asked by At

I'm trying to consume my client API which using an OAuth 1 authentication. I successfully consumed request token and Access token API. So now I have access token.

After that I consume 1 client API in which no extra parameters was there & I'm getting proper response from it.

method of that API:

 public async Task<List<facilityList>> getFacilityList()
{
    try
    {
        // Create a new OAuth client with the required parameters
        var oauth = new OAuthBase();
        var uri = new Uri(facilityListURL);
        var nonce = oauth.GenerateNonce();
        var timeStamp = oauth.GenerateTimeStamp();
        var authenticate = new OAuthAuthentication();
        var generateToken = await authenticate.generateAccessToken().ConfigureAwait(false);
        var signature = oauth.GenerateSignature(uri, CallbackURL, ConsumerKey, ConsumerSecret, generateToken.oauth_token, generateToken.oauth_token_secret, "POST", timeStamp, null, null, null, nonce, out string normalizedUrl, out string normalizedRequestParameters);
        // Build the request URL with the OAuth parameters
        var requestUrl = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + HttpUtility.UrlEncode(signature);

        // Send the request and receive the response
        var request = (HttpWebRequest)WebRequest.Create(requestUrl);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Headers.Add("Authorization", "OAuth " +
            "oauth_consumer_key=\"" + ConsumerKey + "\", " +
            "oauth_nonce=\"" + nonce + "\", " +
            "oauth_signature=\"" + HttpUtility.UrlEncode(signature) + "\", " +
            "oauth_signature_method=\"PLAINTEXT\", " +
             "oauth_timestamp=\"" + timeStamp + "\", " +
            "oauth_token=\"" + generateToken.oauth_token + "\", " +
            "oauth_version=\"1.0\""
            );
        var response = (HttpWebResponse)request.GetResponse();
        var responseText = new StreamReader(response.GetResponseStream()).ReadToEnd();
        List<facilityList> facilityList = JsonConvert.DeserializeObject<List<facilityList>>(responseText);
        //return responseText.ToString();
        return facilityList;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

I'm trying to consume another API in which I need to pass 2 extra parameters in Body->Form-Data but when I'm passing this parameters I'm getting 409 Conflict exception in my C# code & Getting proper response in Postman as below.

enter image description here

Parameters which I'm passing in Form-Data

enter image description here

Below is my C# method

public async Task < string > getAllClass() {
  try {
    // Create a new OAuth client with the required parameters
    var oauth = new OAuthBase();
    var uri = new Uri(classAllURL);
    var nonce = oauth.GenerateNonce();
    var timeStamp = oauth.GenerateTimeStamp();
    var authenticate = new OAuthAuthentication();
    var generateToken = await authenticate.generateAccessToken().ConfigureAwait(false);
    List < facilityList > facilityList = await getFacilityList().ConfigureAwait(false);
    var inputFacilityID = "85331482-a6ab-896b-7f7d-95e9ca570c84"; // facilityList[0].guid.ToString();
    var inputClassType = "in-person";
    var signature = oauth.GenerateSignature(uri, CallbackURL, ConsumerKey, ConsumerSecret, generateToken.oauth_token, generateToken.oauth_token_secret, "POST", timeStamp, null, inputFacilityID, inputClassType, nonce, out string normalizedUrl, out string normalizedRequestParameters);
    // Build the request URL with the OAuth parameters
    var requestUrl = normalizedUrl + "?" + normalizedRequestParameters + "&oauth_signature=" + HttpUtility.UrlEncode(signature);

    // Send the request and receive the response
    var request = (HttpWebRequest) WebRequest.Create(requestUrl);
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.Headers.Add("Authorization", "OAuth " +
      "oauth_consumer_key=\"" + ConsumerKey + "\", " +
      "oauth_nonce=\"" + nonce + "\", " +
      "oauth_signature=\"" + HttpUtility.UrlEncode(signature) + "\", " +
      "oauth_signature_method=\"PLAINTEXT\", " +
      "oauth_timestamp=\"" + timeStamp + "\", " +
      "oauth_token=\"" + generateToken.oauth_token + "\", " +
      "oauth_version=\"1.0\"," +
      "facility_GUID=\"" + inputFacilityID + "\", " +
      "type=\"" + inputClassType + "\""
    );
    var response = (HttpWebResponse) request.GetResponse();
    var responseText = new StreamReader(response.GetResponseStream()).ReadToEnd();
    return responseText;
  } catch (Exception ex) {
    return ex.Message.ToString();
  }
}

How can I resolve the issue?

0

There are 0 answers