V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
dustin2016
V2EX  ›  问与答

请帮忙, PHP 代码/ Java 代码转为 Node 代码或者转为 Python 代码,涉及到字符串格式化和哈希加密函数

  •  
  •   dustin2016 · 2017-10-02 14:51:45 +08:00 · 1446 次点击
    这是一个创建于 2399 天前的主题,其中的信息可能已经有所发展或是发生改变。

    PHP/Java 代码是亚马逊提供的能正常运行的代码,可以返回一个请求 URL

    但是我想部署在 Node 或 python 服务器上,所以需要简单的转换一下,可惜不太懂

    亚马逊只提供了三种格式

    PHP 代码(主要是最后生成规范查询这一块,要格式化字符串,sha256 加密,等,前面的参数无需改变)

    <?php
    
    header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求
    
    // 亚马逊联盟 Access Key
    $access_key_id = "AKIAIVRIFSC2GBUGB4CA";
    
    // 亚马逊联盟 Secret Key
    $secret_key = "nRlIwQc9VBBANdyZ5CNTTGdaXbPzTxN7UX4fZusf";
    
    // 中国亚马逊 (数据来源站)
    // 不同的地区要分别注册该地区的亚马逊联盟
    $endpoint = "webservices.amazon.cn"; 
    
    // 获取数据格式(默认 xml )
    $uri = "/onca/xml";
    
    // 请求参数设置
    $params = array(
    
    	// 服务(适用于所有区域)
        "Service" => "AWSECommerceService",
        // 请求的类型 例如 ItemSearch,ItemLookup 等
        "Operation" => "ItemSearch",
        // 亚马逊联盟 Access Key
        "AWSAccessKeyId" => "AKIAIVRIFSC2GBUGB4CA",
        // 亚马逊跟踪代码 AssociateTag
        "AssociateTag" => "dustin2016-23",
        // 要搜索的产品类别 此处定为书籍 Books  也可以为 ALL,Apparel...等
        "SearchIndex" => "Books",
        // 关键词
        "Keywords" => "js",
        // 响应组过滤请求返回的信息类型 例如,Images 响应组返回项目图像
        "ResponseGroup" => "Large"
    );
    
    // 判断有无时间戳
    if (!isset($params["Timestamp"])) {
    
    	// 如果没有则设置一个当前时间戳
        $params["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
    }
    
    // 对参数进行排序(亚马逊的要求)
    ksort($params);
    
    // 新建一个数组
    $pairs = array();
    
    // 遍历参数并依次放进数组
    foreach ($params as $key => $value) {
        array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
    }
    
    // 生成规范查询
    $canonical_query_string = join("&", $pairs);
    
    // 生成要签名的字符串
    $string_to_sign = "GET\n".$endpoint."\n".$uri."\n".$canonical_query_string;
    
    // 生成产品广告所需的 API
    $signature = base64_encode(hash_hmac("sha256", $string_to_sign, $secret_key, true));
    
    // 生成带签名的 URL
    $request_url = 'http://'.$endpoint.$uri.'?'.$canonical_query_string.'&Signature='.rawurlencode($signature);
    
    // 输出 URL
    echo $request_url
    
    ?>
    
    

    以下是 Java 代码

    package com.amazon.advertising.api.sample;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    
    /*
     * This class shows how to make a simple authenticated call to the
     * Amazon Product Advertising API.
     *
     * See the README.html that came with this sample for instructions on
     * configuring and running the sample.
     */
    public class JavaCodeSnippet {
    
        /*
         * Your Access Key ID, as taken from the Your Account page.
         */
        private static final String ACCESS_KEY_ID = "AKIAIVRIFSC2GBUGB4CA";
    
        /*
         * Your Secret Key corresponding to the above ID, as taken from the
         * Your Account page.
         */
        private static final String SECRET_KEY = "nRlIwQc9VBBANdyZ5CNTTGdaXbPzTxN7UX4fZusf";
    
        /*
         * Use the end-point according to the region you are interested in.
         */
        private static final String ENDPOINT = "webservices.amazon.cn";
    
        public static void main(String[] args) {
    
            /*
             * Set up the signed requests helper.
             */
            SignedRequestsHelper helper;
    
            try {
                helper = SignedRequestsHelper.getInstance(ENDPOINT, ACCESS_KEY_ID, SECRET_KEY);
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
    
            String requestUrl = null;
    
            Map<String, String> params = new HashMap<String, String>();
    
            params.put("Service", "AWSECommerceService");
            params.put("Operation", "ItemSearch");
            params.put("AWSAccessKeyId", "AKIAIVRIFSC2GBUGB4CA");
            params.put("AssociateTag", "dustin2016-23");
            params.put("SearchIndex", "Books");
            params.put("Keywords", "linux");
            params.put("ResponseGroup", "Large");
    
            requestUrl = helper.sign(params);
    
            System.out.println("Signed URL: \"" + requestUrl + "\"");
        }
    }
    
    
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1282 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:52 · PVG 07:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.