使用 OAuth1.0 调用 REST Api 到 C# 应用程序上的 netsuite SuiteTalk
Call REST Api using OAuth1.0 to netsuite SuiteTalk on C# application
现在我正在尝试使用 RestSharp 连接到 C# 应用程序上的 Netsuite SuiteTalk 的其余 api。
但它 returns 未经授权的错误(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 ....
}
我认为签名功能是正确的。但是我无法弄清楚401错误的原因。
我在代码中缺少什么?有经验的朋友帮帮我吧
我猜 realm="account number" 丢失了,尝试更新签名中的 realm
我终于找到问题了。我不应该添加以下行
request.AddQueryParameter("q", query);
当我使用 netsuite_base_url 字符串时,url 必须是小写。
现在我正在尝试使用 RestSharp 连接到 C# 应用程序上的 Netsuite SuiteTalk 的其余 api。 但它 returns 未经授权的错误(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 ....
}
我认为签名功能是正确的。但是我无法弄清楚401错误的原因。 我在代码中缺少什么?有经验的朋友帮帮我吧
我猜 realm="account number" 丢失了,尝试更新签名中的 realm
我终于找到问题了。我不应该添加以下行
request.AddQueryParameter("q", query);
当我使用 netsuite_base_url 字符串时,url 必须是小写。