簡體   English   中英

在 C# 應用程序上使用 OAuth1.0 to.netsuite SuiteTalk 調用 REST Api

[英]Call REST Api using OAuth1.0 to netsuite SuiteTalk on C# application

現在我正在嘗試使用 RestSharp 在 C# 應用程序上連接到 Netsuite SuiteTalk 的 rest api。 但它返回未經授權的錯誤(401)。 (使用 Postman 調用 api 有效)我的代碼如下。

public string generateSignature(string httpMethod, string timestamp, string nonce, 
                                        SortedDictionary<string, string> data)
{
        var parameters = new SortedDictionary<string, string>
        {
           {"oauth_consumer_key", mConfig.ConsumerKey},
           { "oauth_token", mConfig.TokenId },
           {"oauth_signature_method", "HMAC-SHA256"},
           {"oauth_timestamp", timestamp},
           {"oauth_nonce", nonce},
           {"oauth_version", "1.0"}
        };
        foreach (KeyValuePair<string, string> elt in data)
        {
             parameters.Add(elt.Key, elt.Value);
        }
        string Signature_Base_String = httpMethod;
        Signature_Base_String = Signature_Base_String.ToUpper();
        Signature_Base_String = Signature_Base_String + "&";
        string PercentEncodedURL = Uri.EscapeDataString(netsuite_base_url);
        Signature_Base_String = Signature_Base_String + PercentEncodedURL;
        Signature_Base_String = Signature_Base_String + "&";
        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);
            }
        }
        string key = mConfig.ConsumerSecret + "&" + mConfig.TokenSecret;
        string signature = "";
        var encoding = new System.Text.ASCIIEncoding();
        byte[] keyByte = encoding.GetBytes(key);
        byte[] messageBytes = encoding.GetBytes(Signature_Base_String);
        using (var myhmacsha256 = new HMACSHA256(keyByte))
        {
            byte[] hashmessage = myhmacsha256.ComputeHash(messageBytes);
            signature = Uri.EscapeDataString(Convert.ToBase64String(hashmessage));
        }
        return signature;
}
public bool getData()
{
   string timestamp = computeTimestamp();// ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
   string nonce = Convert.ToBase64String(Encoding.UTF8.GetBytes(timestamp));// computeNonce();
   var data = new SortedDictionary<string, string>{
         { "q", query}
      };
   string signature = generateSignature("GET", timestamp, nonce, data);
   netSuiteAuthorization = "OAuth " +
        "realm=" + "\""+mConfig.AccountId + "\"" + "," +
        "oauth_consumer_key=" + "\"" + mConfig.ConsumerKey + "\"" + "," +
        "oauth_token=" + "\"" + mConfig.TokenId + "\"" + "," +
        "oauth_signature_method=" + "\"" + SIGNATURE_METHOD + "\"" + "," +
        "oauth_timestamp=" + "\"" + timestamp + "\"" + "," +
        "oauth_nonce=" + "\"" + nonce + "\"" + "," +
        "oauth_version=" + "\"" + "1.0" + "\"" + "," +
        "oauth_signature= " + "\"" + signature + "\"";
       var client = new RestClient(netsuite_base_url);
       var request = new RestRequest(Method.GET);
       request.AddHeader("Authorization", netSuiteAuthorization);
       request.AddHeader("Content-Type", "application/json");
       request.AddQueryParameter("q", query);
       IRestResponse response = client.Execute(request);
       ... do something ....
}

我認為簽名 function 是正確的。 但是我無法弄清楚401錯誤的原因。 我在代碼中缺少什么? 如果有人有經驗,請幫助我。

我猜 realm="account number" 丟失,嘗試更新簽名中的 realm

我終於找到了問題。 我不應該添加以下行

request.AddQueryParameter("q", query);

而當我使用 netsuite_base_url 字符串時,url 必須是小寫的。

示例: RestSharp版本:108.0.3

不需要編寫簽名創建代碼。 RestSharp 為您做這一切。

using System;
using RestSharp;
using RestSharp.Authenticators;
using RestSharp.Authenticators.OAuth;
using Newtonsoft.Json;


var client = new RestClient(URL);
var oAuth1 = OAuth1Authenticator.ForAccessToken(
                consumerKey: ConsumerKey,
                consumerSecret: ConsumerSecret,
                token: Token,
                tokenSecret: TokenSecret,
                OAuthSignatureMethod.HmacSha256);
oAuth1.Realm = Realm; // if Realm has otherwise ignore

client.Authenticator = oAuth1;

var request = new RestRequest(URL, Method.Post);
request.AddHeader("Content-Type", "application/json");  
string body = JsonConvert.SerializeObject(bodyObject);
           
request.AddParameter("application/json", body, ParameterType.RequestBody);
var response = client.Execute(request);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM