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

2017-10-02 14:51:45 +08:00
 dustin2016

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 + "\"");
    }
}

1454 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/395147

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX