I'm trying to get a request to work for TBA web services. I was having issues and tried to verify what I was doing by running the NetSuite example for TBA through my own code to ensure my signature process was being performed correctly.
They show the following code in SuiteAnswer 44710:
String sCompId = "1234567";
String consumerKey = "71cc02b731f05895561ef0862d71553a3ac99498a947c3b7beaf4a1e4a29f7c4";
String consumerSecret = "7278da58caf07f5c336301a601203d10a58e948efa280f0618e25fcee1ef2abd";
String tokenId = "89e08d9767c5ac85b374415725567d05b54ecf0960ad2470894a52f741020d82";
String tokenSecret = "060cd9ab3ffbbe1e3d3918e90165ffd37ab12acc76b4691046e2d29c7d7674c2";
String algorithm = SignatureAlgorithm._HMAC_SHA256;
and the following example header:
<ns:tokenPassport soap:actor="http://schemas.xmlsoap.org/soap/actor/next" soap:mustUnderstand="0" xmlns:ns="urn:messages_2015_2.platform.webservices.netsuite.com"> <ns:account>1234567</ns:account> <ns:consumerKey>71cc02b731f05895561ef0862d71553a3ac99498a947c3b7beaf4a1e4a29f7c4</ns:consumerKey> <ns:token>89e08d9767c5ac85b374415725567d05b54ecf0960ad2470894a52f741020d82</ns:token> <ns:nonce>6obMKq0tmY8ylVOdEkA1</ns:nonce> <ns:timestamp>1439829974</ns:timestamp> <ns:signature algorithm="HMAC_SHA256">fzGxUBu6SZvGqv5hk8P4ou2DPthSxXtJ4zJIeCBQK5A=</ns:signature> </ns:tokenPassport>
You create the signature by running a base string and key into a HMAC SHA256 function and then base 64 encoding it. There is sample code in the suiteanswerString baseString = sCompId+ "&" + consumerKey + "&" + tokenId + "&" + nonce + "&" + timeStamp;1234567&71cc02b731f05895561ef0862d71553a3ac99498a947c3b7beaf4a1e4a29f7c4&89e08d9767c5ac85b374415725567d05b54ecf0960ad2470894a52f741020d82&6obMKq0tmY8ylVOdEkA1&1439829974String key = consumerSecret + '&' + tokenSecret;7278da58caf07f5c336301a601203d10a58e948efa280f0618e25fcee1ef2abd&060cd9ab3ffbbe1e3d3918e90165ffd37ab12acc76b4691046e2d29c7d7674c2Using Python, Node and their own example code on the suiteanswer I always get the same answer which doesn't match their signature. I've also tried different text and url encodings and still can't get their answer. Is it an invalid signature for what they have? Its driving me nutsmy signature: FCghIZqXNetuZY8ILWOFH0ucdfzQOmAuL+q+kF21zPs=their signiature: fzGxUBu6SZvGqv5hk8P4ou2DPthSxXtJ4zJIeCBQK5A=Python: import hmac import hashlib import base64 import urllib baseStringStr = u'1234567&71cc02b731f05895561ef0862d71553a3ac99498a947c3b7beaf4a1e4a29f7c4&89e08d9767c5ac85b374415725567d05b54ecf0960ad2470894a52f741020d82&6obMKq0tmY8ylVOdEkA1&1439829974' keyStr = u'7278da58caf07f5c336301a601203d10a58e948efa280f0618e25fcee1ef2abd&060cd9ab3ffbbe1e3d3918e90165ffd37ab12acc76b4691046e2d29c7d7674c2' baseString = baseStringStr.encode('utf-8') key = keyStr.encode('utf-8') dig = hmac.new(key, msg=baseString, digestmod=hashlib.sha256).digest() print(base64.b64encode(dig).decode()) Java: import java.security.*; import java.util.Base64; import java.util.Date; import org.apache.commons.lang3.RandomStringUtils; import javax.crypto.*; import javax.crypto.spec.SecretKeySpec; import javax.xml.*; public class createSignature{ public static void main(String[] args) { try { testGetServerTimeWithTBA(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String computeSignature( String account, String consumerKey, String consumerSecret, String token, String tokenSecret, String nonce, Long timeStamp, String signatureAlgorithm ) throws Exception { String baseString = account + "&" + consumerKey + "&" + token + "&" + nonce + "&" + timeStamp; String key = consumerSecret + '&' + tokenSecret; String signature; if (signatureAlgorithm.equals("HmacSHA256")) { signature = computeShaHash(baseString, key, "HmacSHA256"); } else { signature = computeShaHash(baseString, key, "HmacSHA1"); } return signature; } public static void testGetServerTimeWithTBA() throws Exception { // Constants String sCompId = "1234567"; String consumerKey = "71cc02b731f05895561ef0862d71553a3ac99498a947c3b7beaf4a1e4a29f7c4"; String consumerSecret = "7278da58caf07f5c336301a601203d10a58e948efa280f0618e25fcee1ef2abd"; String tokenId = "89e08d9767c5ac85b374415725567d05b54ecf0960ad2470894a52f741020d82"; String tokenSecret = "060cd9ab3ffbbe1e3d3918e90165ffd37ab12acc76b4691046e2d29c7d7674c2"; String algorithm = "HmacSHA256"; // Nonce and timeStamp must be unique for each request. Also, make sure that the nonce value you // generate does not contain special characters. // String nonce = RandomStringUtils.randomAlphanumeric(20); String nonce = "6obMKq0tmY8ylVOdEkA1"; // Long timeStamp = System.currentTimeMillis() / 1000L; Long timeStamp = 1439829974L; String signature = computeSignature(sCompId, consumerKey, consumerSecret, tokenId, tokenSecret, nonce, timeStamp, algorithm); // create and set TokenPassport SOAP Header // notice - no secrets are included in this call System.out.println(signature); } protected static String computeShaHash(String baseString, String key, String algorithm) throws Exception { if (!algorithm.equals("HmacSHA256") && !algorithm.equals("HmacSHA1")) algorithm = "HmacSHA1"; { byte[] bytes = key.getBytes(); SecretKeySpec mySigningKey = new SecretKeySpec(bytes, algorithm); Mac messageAuthenticationCode = Mac.getInstance(algorithm); messageAuthenticationCode.init(mySigningKey); byte[] hash = messageAuthenticationCode.doFinal(baseString.getBytes()); byte[] encoded = Base64.getEncoder().encode(hash); return new String(encoded); } } }