OAuth1.0 身份验证显示“无效签名 - 提供的签名不匹配。”错误 401

时间:2021-04-26 09:21:05

标签: c# rest oauth webapi gravityforms

OAuth1.0 身份验证说:

<块引用>

无效签名 - 提供的签名不匹配。” 错误 401

我想从 GravityForm 提供的 API 获取数据,它完全适用于 Postman,但我无法通过我自己的 C# 应用程序获取它。 我在 Stackoverflow 上尝试了朋友提供的不同解决方案,但不幸的是,它们都不起作用。 这是我的最终代码:

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;
    }

尝试过的解决方案: 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 个答案:

答案 0 :(得分:0)

哦,大约一个月后,我终于找到了解决方案。对于 GravityForm API 的身份验证,OAUTH 1.0 并不像您预期​​的那样稳定。相反,您最好像这样使用“基本身份验证”:

serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort2.DataReceived += new SerialDataReceivedEventHandler(serialPort2_DataReceived);

void button1(object sender, EventArgs e)
{
    try
    {
        serialPort1.PortName = comboBox1.Text;
        serialPort1.BaudRate = Convert.ToInt32(comboBox2.Text);
        serialPort1.DataBits = Convert.ToInt32(comboBox3.Text);
        serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits),     comboBox4.Text);
        serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox5.Text);
        serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
        serialPort1.Open();
        progressBar1.Value = 100;
    }

    catch (Exception err)
    {
        MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}
void button7_Click(object sender, EventArgs e)
{
    try
    {  
        serialPort2.PortName = comboBox16.Text;
        serialPort2.BaudRate = Convert.ToInt32(comboBox15.Text);
        serialPort2.DataBits = Convert.ToInt32(comboBox13.Text);
        serialPort2.StopBits = (StopBits)Enum.Parse(typeof(StopBits), comboBox14.Text);
        serialPort2.Parity = (Parity)Enum.Parse(typeof(Parity), comboBox12.Text);
        serialPort2.DataReceived += new SerialDataReceivedEventHandler(serialPort2_DataReceived);
        serialPort2.Open();
        progressBar2.Value = 100;
    }
    catch (Exception err)
    {
        MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

然后您可以使用以下解决方案: RestSharp HttpBasicAuthentication - example