使用Quickbooks REST API创建客户,获取错误401

本文关键字:获取 取错误 客户 创建 Quickbooks REST API 使用 | 更新日期: 2023-09-27 18:00:57

我正在尝试使用QuickBooks REST API在QuickBooks上创建客户,我尝试按照下面的编写代码

var customer = _custtomer;
        var oauth_token = "************";
        var oauth_consumer_key = "**************";
        var oauth_consumer_secret = "**************";
        var oauth_version = "1.0";
        var oauth_signature_method = "HMAC-SHA1";
        var realmId = "*******";
        var oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
        var timeSpan = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
        var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
        var resource_url = "https://qb.sbfinance.intuit.com/v3/company/" + realmId + "/customer";
        var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";
        var baseString = string.Format(baseFormat,
                                    oauth_consumer_key,
                                    oauth_nonce,
                                    oauth_signature_method,
                                    oauth_timestamp,
                                    oauth_token,
                                    oauth_version,
                                    Uri.EscapeDataString(customer)
                                    );

        baseString = string.Concat("POST&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));
        var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret), "&", " " );
        string oauth_signature;
        using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
        {
            oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
        }
        var header = "OAuth oauth_token='"{0}'",oauth_nonce='"{1}'",oauth_consumer_key='"{2}'",oauth_signature_method='"{3}'"," +
            "oauth_timestamp='"{4}'",oauth_version='"{5}'",oauth_signature='"{6}";
        var authHeader = string.Format(header,
                                Uri.EscapeDataString(oauth_token),
                                Uri.EscapeDataString(oauth_nonce),
                                Uri.EscapeDataString(oauth_consumer_key),
                                Uri.EscapeDataString(oauth_signature_method),
                                Uri.EscapeDataString(oauth_timestamp),
                                Uri.EscapeDataString(oauth_version),
                                Uri.EscapeDataString(oauth_signature)
                        );
        var postBody = Uri.EscapeDataString(customer);
        ServicePointManager.Expect100Continue = true;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
        request.Accept = "application/json";
        request.Headers.Add("Authorization", authHeader);
        request.Method = "POST";
        request.ContentType = "application/Json";
        request.Host = "qb.sbfinance.intuit.com";
        using (Stream stream = request.GetRequestStream())
        {
            byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
            stream.Write(content, 0, content.Length);
        }
        WebResponse response = request.GetResponse();
        string responceString = new StreamReader(response.GetResponseStream()).ReadToEnd(); 

但我认为我在代码中遗漏了一些内容,或者做得不对,它给出了错误401未经授权的请求,提前感谢。

使用Quickbooks REST API创建客户,获取错误401

您的oauth签名不正确。401是令牌拒绝错误。请在此处查看我的答案-https://intuitdeveloper.lc.intuit.com/questions/1184667-creating-oauth-for-rest-api-it-gives-an-error-of-the-remote-server-returned-an-error-401-unauthorized-i-checked-my-access-token-and-consumer-key-secrete-they-r-ok