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

E-Trade API 签名失败

如何解决E-Trade API 签名失败

不幸的是,电子贸易 API 文档不是很清楚,而且我很难为 OAuth 身份验证生成有效的签名。我使用的是 .NET 5(核心)。

这是我得到的:

HTTP Status 401 - oauth_problem=signature_invalid

有人知道生成签名的规范是什么吗?

这是我尝试过的:

 private static string GetSignatureBaseString(string strUrl,string TimeStamp,string Nonce,string strConsumer,string strOauthToken,SortedDictionary<string,string> data)
    {
        //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.
        string PercentEncodedURL = Uri.EscapeDataString(strUrl);
        Signature_Base_String = Signature_Base_String + PercentEncodedURL;

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

        //5.append OAuth parameter string to the output string.
        var parameters = new SortedDictionary<string,string>
        {
            {"oauth_consumer_key",strConsumer},{"oauth_timestamp",TimeStamp},{"oauth_nonce",Nonce},{"oauth_signature_method","HMAC-SHA1"},{"oauth_callback","oob"},{"oauth_version","1.0"}
        };

        bool first = true;
        foreach (keyvaluePair<string,string> elt in parameters)
        {
            if (first)
            {
                Signature_Base_String = Signature_Base_String + Uri.EscapeDataString(elt.Key + "=" + elt.Value);
                first = false;
            }
            else
            {
                Signature_Base_String = Signature_Base_String + Uri.EscapeDataString("&" + elt.Key + "=" + elt.Value);
            }
        }

        return Signature_Base_String;
    }

 private static string GetSha1Hash(string key,string baseString)
    {
        var encoding = new System.Text.ASCIIEncoding();

        byte[] keyBytes = encoding.GetBytes(key);
        byte[] messageBytes = encoding.GetBytes(baseString);

        string strSignature = string.Empty;

        using (HMACSHA1 SHA1 = new HMACSHA1(keyBytes))
        {
            var Hashed = SHA1.ComputeHash(messageBytes);
            strSignature = Convert.ToBase64String(Hashed);
        }

        return strSignature;
    }

这是我如何进行非常基本的调用

TimeSpan t = DateTime.UtcNow - new DateTime(1970,1,1);
        int secondsSinceEpoch = (int)t.TotalSeconds;

        var nonce = "kllo9940pd9333jh";

        var baseString = GetSignatureBaseString("https://api.eTrade.com/oauth/request_token",secondsSinceEpoch.ToString(),nonce.ToString(),"XXX",string.Empty,null);
        var signature = GetSha1Hash("XXX",baseString);

        var headers = "OAuth realm=,oauth_callback=\"oob\",oauth_signature=\"" + HttpUtility.UrlEncode(signature) + "\",oauth_nonce=\"" + nonce + "\",oauth_signature_method=\"HMAC-SHA1\",oauth_consumer_key=\"XXX\",oauth_timestamp=\"" + secondsSinceEpoch.ToString() + "\"";

        using (var http = new HttpClient())
        {
            http.DefaultRequestHeaders.Add("Authorization",headers);

            var httpResponse = http.GetAsync("https://api.eTrade.com/oauth/request_token").Result;
            var httpContent = httpResponse.Content.ReadAsstringAsync().Result;
            return httpContent;
        }

不幸的是,到目前为止,我尝试过的一切都没有奏效。感谢您的帮助。

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?