繁体   English   中英

使用Google Apps脚本访问Amazon MWS

[英]Accessing Amazon MWS using Google Apps Script

我以某种方式设法按照文档编写代码。 但不幸的是,它不起作用。 下面是代码,请看一下,请让我知道我在做错什么或缺少字段。 我已经通过引用某人的代码编写了此代码。

function test(){

  var date = new Date(); 
  var amz_date  = date.toISOString(); //2018-05-01T20:40:50.940Z

  var yearMonthDay= Utilities.formatDate(date, 'UTC', 'yyyyMMdd');
  var hourMinuteSec = Utilities.formatDate(date, 'UTC', 'HHmmss');

  var dateForStringToSign = yearMonthDay +'T'+hourMinuteSec+'Z'; //20180501T204050Z

  var datestamp = yearMonthDay; // 20180501

  var access_key = "xxxxxxxxxxxxx";
  var secret_key = "xxxxxxxxxxxxx";

  var method = 'POST';
  var host = 'mws.amazonservices.com';
  var endpoint = 'https://mws.amazonservices.com/Orders/2013-09-01';  
  var canonical_uri = '/Orders/2013-09-01';
  var canonical_querystring =  'AWSAccessKeyId='+encodeURIComponent('xxxxxxxxxxxxx');
      canonical_querystring += '&Action='+encodeURIComponent('GetOrder');
      canonical_querystring += '&AmazonOrderId.Id.1='+encodeURIComponent('xxxxxxxxxxxxx');
      canonical_querystring += '&MWSAuthToken='+encodeURIComponent('xxxxxxxxxxxxx');
      canonical_querystring += '&SellerId='+encodeURIComponent('xxxxxxxxxxxxx');
      canonical_querystring += '&SignatureMethod='+encodeURIComponent('HmacSHA256');
      canonical_querystring += '&SignatureVersion='+encodeURIComponent('2');
      canonical_querystring += '&Timestamp='+encodeURIComponent(amz_date);
      canonical_querystring += '&Version='+encodeURIComponent('2013-09-01');


   //To construct the finished canonical request, combine all the components
   var finished_canonical= method + "\n"+host+"\n" +canonical_uri+ "\n" + canonical_querystring;

   //Calculate the Signature
   var signature=getSignature(finished_canonical,secret_key); 

   //Add signature to querystring
   canonical_querystring += '&Signature='+encodeURIComponent(signature);

   Logger.log(canonical_querystring);
   var request_url = endpoint + '?' + canonical_querystring;
   var options = {

      'method' : 'post',
      'muteHttpExceptions':true,
      'host': host,

   }
   var x = UrlFetchApp.fetch(request_url, options);
   Logger.log(x);
 }

这是GAS中的签名生成器功能,

//Return Signatue (hash) from HmacSha256 
function getSignature(message,secret){
  var byteSignature = Utilities.computeHmacSha256Signature(message, secret);
  // convert byte array to hex string
  var signature = byteSignature.reduce(function(str,chr){
    chr = (chr < 0 ? chr + 256 : chr).toString(16);
    return str + (chr.length==1?'0':'') + chr;
  },'');
  return signature;
}

如您在代码中所见,生成签名后,将其作为最后一个参数附加到查询字符串中。 那会是个问题吗?

阅读此处称为“如果您创建自己的客户端库”的部分。 它讨论了格式化URL和签名。

此外,这是一个可能有用的链接: https : //stackoverflow.com/a/36417555/3047

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM