如何使用Apps Script生成NetSuite请求签名?
问题:NetSuite OAuth签名生成失败导致INVALID_LOGIN_ATTEMPT错误
错误返回内容:
{"error" : {"code" : "INVALID_LOGIN_ATTEMPT", "message" : "Invalid login attempt."}}
我用Apps Script发送GET请求调用NetSuite接口时,卡在签名生成环节,查阅官方文档后仍无法解决,每次都返回上面的登录失败错误。
我怀疑问题出在text和key参数的编码及构造逻辑上,具体疑问点:
- 不确定哪些参数需要UTF-8编码,哪些需要Base64编码;
- 已知key由consumer secret和token secret拼接而成,但不清楚编码操作是在拼接前还是拼接后执行;
- 已按照文档示例格式构造text参数(示例格式:
POST&https%3A%2F%2F1234567.restlets.api.netsuite.com%2Frest%2Frequesttoken&oauth_callback%3Dhttps%253A%252F%252Fmy.example.com%252FTBA%252F%253FcallbackRequest%26oauth_consumer_key%3D60712990bc09623786e7047c226bcb3f86d49dca0b04efc21001dc76d97a81f5%26oauth_nonce%3DbUvpxBX93OWo0FLswq5M%26oauth_signature_method%3DHMAC-SHA256%26oauth_timestamp%3D1575998103%26oauth_version%3D1.0%26role%3D45678),但不确定是否还需要额外编码。
当前使用的Apps Script代码
function getRequest(){ let url = ""; let marcaDeTiempo = timeEstampd(); //this is the timestamp let text = ""; let consumerSecret = ""; let tokenSecret = ""; let key = consumerSecret + "&" + tokenSecret; let signature = crypto(text, key); var formData = { 'Authorization': `OAuth realm=\"\",oauth_consumer_key=\"\",oauth_token=\"\",oauth_signature_method=\"HMAC-SHA256\",oauth_timestamp=\"${marcaDeTiempo}\",oauth_nonce=\"\",oauth_version=\"1.0\",oauth_signature=\"${signature}\"`, 'Cookie': '', 'Content-type': 'application/json' }; var options = { method : 'get', headers : formData, "muteHttpExceptions": true }; let response = UrlFetchApp.fetch(url, options); Logger.log(response); } //function to create the signature function crypto(message, secret){ var byteSignature = Utilities.computeHmacSha256Signature(message, secret); // convert byte array to hex string Charset.UTF_8 var signature = byteSignature.reduce(function(str,chr){ chr = (chr < 0 ? chr + 256 : chr).toString(16); return str + (chr.length==1?'0':'') + chr; },'' ); //Logger.log(typeof signature); return Utilities.base64EncodeWebSafe(byteSignature); } //function to generate timestamp function timeEstampd(){ const currentTime = new Date(); return currentTime.getTime(); }
内容的提问来源于stack exchange,提问作者P CP
相关产品推荐
相关产品推荐

