0

OAuth1.0 authentication says:

Invalid signature - provided signature does not match." Error 401

I wanted to get data from an API provided by GravityForm, it completely works in Postman, but I can't get it through my own C# app. I have tried different solutions provided by friends on Stackoverflow, but unfortunately, none of them work. Here's my ultimate code:

private void button2_Click(object sender, EventArgs e)
{
   string sig = GetAuthorizationToken();
   ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
   var client = new RestClient("https://kashfsho.com/wp-json/gf/v2/entries/");
   var request = new RestRequest(Method.GET);
   request.AddHeader("cache-control", "no-cache");
   request.AddHeader("Content-Type", "application/json");
   request.AddHeader("authorization", sig);
   IRestResponse response = client.Execute(request);
}

public string GetAuthorizationToken()
{
    timeStamp = ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
    nonce = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(timeStamp + timeStamp + timeStamp));
    ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://kashfsho.com/wp-json/gf/v2/entries/");
    httpWebRequest.Method = "Get";
    string consumer_secret = Uri.EscapeDataString(conSumerSecret);
    string signature_base_string = GetSignatureBaseString(timeStamp, nonce);
    SHA1HASH = GetSha1Hash(signature_base_string, consumer_secret); //also tried with nonEscaped consumer!
    string Header =
       "OAuth oauth_consumer_key=" + '"' + consumerKey + '"' + "," +
       "oauth_nonce=" + '"' + nonce + '"' + "," +
       "oauth_signature=" + '"' + SHA1HASH + '"' + "," +
       "oauth_signature_method=" + '"' + @"HMAC-SHA1" + '"' + "," +
       "oauth_timestamp=" + '"' + timeStamp + '"' + "," +
       "oauth_version=" + '"' + "1.0" + '"';
    return Header;
}

private string GetSha1Hash(string key, string t)
{
    var encoding = new System.Text.ASCIIEncoding();
    byte[] keyBytes = encoding.GetBytes(key);
    byte[] messageBytes = encoding.GetBytes(t);
    string strSignature = string.Empty;
    using (HMACSHA1 SHA1 = new HMACSHA1(keyBytes))
    {
        var Hashed = SHA1.ComputeHash(messageBytes);
        SHA1HASH = Convert.ToBase64String(Hashed);
    }
    return SHA1HASH;
}
    public string GetSignatureBaseString(string TimeStamp, string Nonce)
    {
        //1.Convert the HTTP Method to uppercase and set the output string equal to this value.
        string Signature_Base_String = "Get";
        Signature_Base_String = Signature_Base_String.ToUpper();

        //2.Append the ‘&’ character to the output string.
        Signature_Base_String = Signature_Base_String + "&";

        //3.Percent encode the URL and append it to the output string.
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        string PercentEncodedURL = Uri.EscapeDataString("https://kashfsho.com/wp-json/gf/v2/entries/");
        Signature_Base_String = Signature_Base_String + PercentEncodedURL;

        //4.Append the ‘&’ character to the output string.
        Signature_Base_String = Signature_Base_String + "&";

        //5.append parameter string to the output string.
        Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("oauth_consumer_key=" + consumerKey);
        Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_nonce=" + Nonce);
        Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_signature_method=" + "HMAC-SHA1");
        Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_timestamp=" + TimeStamp);
        Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&oauth_version=" + "1.0");



        return Signature_Base_String;
    }

Tried Solutions: Generate OAuth1 signature in C# Create OAuth Signature with HMAC-SHA1 Encryption returns HTTP 401 Using C# to create the Twitter authorization header for search

1 Answers1

0

Oh, after about a month, I finally reached a solution. For authentication of GravityForm APIs, OAUTH 1.0 is not as stable as you may expect. Instead, you'd better use "BASIC AUTHENTICATION" like this:

USERNAME = consumerKey
Password = consumerSecret

Then you can use following solution: RestSharp HttpBasicAuthentication - example