微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

解决方法

哦,大约一个月后,我终于找到了解决方案。对于 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,MessageBoxIcon.Error);
    }
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。