Hello,
I'm new to NetSuite and have been fighting with trying to connect to the REST API using C# for a while now. I've downloaded the Postman collection and all of my calls are successful there, so I know I have the environment variables set up correctly. However, when I try in my actual code I get this error:
Invalid login attempt. For more details, see the Login Audit Trail
And when I check the audit log I get this message, which unfortunately doesn't help me pinpoint what's incorrect with the signature:
InvalidSignature
I've backed up a step, and although I know I can't hardcode the nonce and timestamp for my actual RestSharp call, I've temporarily hardcoded them just to see if I can get just the signature itself to match what's showing in Postman. (I'm not actually sure this is valid anyway, maybe it's not possible to generate the same signature twice the way this works?)
I've looked at https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_1534941088.html, which unfortunately isn't in C#, as well as several examples online... not only do they all seem to generate an incorrect signature, but none of the examples even generate the same signature as each other.
I also looked at https://community.oracle.com/netsuite/discussion/comment/15557741#Comment_15557741, which didn't work for me. My application uses .NET framework 4.6.1, if that makes a difference.
I've lost track of where I got this example from, but this one seems to get the closest (consumer and token variables have been removed):
string baseAPI = "https://[account id removed]-sb1.suitetalk.api.netsuite.com/services/rest/record/v1/metadata-catalog/customer";
string consumerKey = [consumerKey];
string tokenId = [tokenID];
string consumerSecret = [consumerSecret];
string tokenSecret = [tokenSecret];
string timestamp = tempTimestamp; //temporarily hardcoded to match Postman call
string nonce = tempNonce; //temporarily hardcoded to match Postman call
string version = "1.0";
string method = "HMAC-SHA256";
string httpMethod = "GET";
string realm = "[account id removed]_SB1";
var parameters = new SortedDictionary<string, string>
{
{"oauth_consumer_key", consumerKey},
{"oauth_token", tokenId },
{"oauth_signature_method", method},
{"oauth_timestamp", timestamp},
{"oauth_nonce", nonce},
{"oauth_version", version}
};
string Signature_Base_String = httpMethod;
Signature_Base_String = Signature_Base_String.ToUpper();
Signature_Base_String = Signature_Base_String + "&";
string PercentEncodedURL = Uri.EscapeDataString(baseAPI);
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 = consumerSecret + "&" + 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));
}
Any help is much appreciated, thank you!