全部产品

Node.js SDK

本文介绍淘宝联盟服务Node.js SDK的使用方法及示例。

接口列表

接口名称

接口说明

SearchByPic

用于向淘宝联盟版在线调用,根据图片查询。

SearchByUrl

用户向淘宝联盟版在线调用,根据图片Url查询

准备工作

  • 在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见

    创建AccessKey

  • 安装Node.js SDK

npm install @alicloud/imagesearch20210501@1.0.1 --save

SearchByPic接口

  • 代码示例

const oss = require('@alicloud/oss-util');
const fs = require('fs');
const imagesearch = require('@alicloud/imagesearch20210501')

const client = new imagesearch.default({
    accessKeyId: "xxxxxx",
    accessKeySecret: "xxxxxxx",
    type: "access_key",
    endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
    regionId: "cn-shanghai",
    protocol: 'http'
});
var PIC_NAME = "C:\xxxxx/9.jpg";

// 根据图片查询
async function SearchPic() {
    var picContent = fs.createReadStream(PIC_NAME);
    const request = new imagesearch.SearchByPicAdvanceRequest({
        // 必填,图像搜索实例名称。
        // 图片大小要求不超过4 MB。格式支持PNG、JPG、JPEG、BMP、TIFF、PPM、WEBP。
        picContentObject: picContent,
        // 需要返回的字段list。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
        fields:"Provcity,UserType,CouponTotalCount,ItemId",
        // 图片类目
        //categoryId : 88888888,
        // 是否需要进行主体识别。默认true
        //crop : false,
        // 图片的主体区域,格式为x1,x2,y1,y2
        //region : "518,1524,398,1632",
        // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
        start : 0,
        // 选填,返回结果的数目。取值范围:1-20。默认值:10。
        num : 1
        // 渠道ID。用于淘宝联盟中的渠道区分。
        //relationId : 1145;
    });
    let runtimeOptions = new oss.RuntimeOptions({});
    const addResponse = await client.searchByPicAdvance(request, runtimeOptions);
    console.log(1, addResponse);
    console.log(2, addResponse.body.data.toMap());
}
console.log("开始查询")
SearchPic();

  • 结果示例

1 SearchByPicResponse {
  headers: {
    date: 'Tue, 08 Jun 2021 08:18:34 GMT',
    'content-type': 'application/json;charset=utf-8',
    'content-length': '932',
    connection: 'keep-alive',
    'access-control-allow-origin': '*',
    'access-control-allow-methods': 'POST, GET, OPTIONS',
    'access-control-allow-headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signature',
    'access-control-max-age': '172800',
    'x-acs-request-id': '457B2A30-3C17-4905-AEB3-938D110C54A6'
  },
  body: SearchByPicResponseBody {
    success: true,
    code: 0,
    data: SearchByPicResponseBodyData { auctions: [Array] },
    requestId: '457B2A30-3C17-4905-AEB3-938D110C54A6',
    picInfo: SearchByPicResponseBodyPicInfo {
      mainRegion: [SearchByPicResponseBodyPicInfoMainRegion],
      multiRegion: [Array]
    }
  }
}
2 [
  SearchByPicResponseBodyDataAuctions {
    result: SearchByPicResponseBodyDataAuctionsResult {
      itemId: '558112896177',
      itemName: 'PPR三通接头配件',
      pic: '//img.alicdn.com/i1/xxxxx.jpg',
      price: '0.12',
      promotionPrice: '0.12',
      userType: 0,
      provcity: '山东 临沂',
      clickUrl: '//s.click.taobao.com/t?yyyyy'
    },
    rankScore: 0.8301
  }
]
3 [
  SearchByPicResponseBodyPicInfoMultiRegion { region: '55,456,65,415' }
]
3 SearchByPicResponseBodyPicInfoMainRegion {
  region: '55,456,65,415',
  multiCategoryId: [
    SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 88888888,
      score: 0.7559
    },
    SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 20,
      score: 0.05169
    },
    SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 8,
      score: 0.04217
    },
    SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 9,
      score: 0.03845
    }
  ]
}

SearchByUrl接口

  • 代码示例

const oss = require('@alicloud/oss-util');
const fs = require('fs');
const imagesearch = require('@alicloud/imagesearch20210501')

const client = new imagesearch.default({
    accessKeyId: "xxxxxx",
    accessKeySecret: "xxxxxx",
    type: "access_key",
    endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
    regionId: "cn-shanghai",
    protocol: 'http'
});

var PIC_URL = "https://ossname.oss-cn-shanghai.aliyuncs.com/m.jpg";

// 根据图片url查询
async function SearchUrl() {
    const request = new imagesearch.SearchByUrlRequest({
        // 必填,图像搜索实例名称。
        // 图片大小要求不超过4 MB。格式支持PNG、JPG、JPEG、BMP、TIFF、PPM、WEBP。
        picUrl: PIC_URL,
        // 需要返回的字段list。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
        fields:"Provcity,UserType,CouponTotalCount,ItemId",
        // 图片类目
        //categoryId : 88888888,
        // 是否需要进行主体识别。默认true
        //crop : false,
        // 图片的主体区域,格式为x1,x2,y1,y2
        //region : "518,1524,398,1632",
        // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
        start : 0,
        // 选填,返回结果的数目。取值范围:1-20。默认值:10。
        num : 1
        // 渠道ID。用于淘宝联盟中的渠道区分。
        //relationId : 1145;
    });
    const addResponse = await client.searchByUrl(request);
    console.log(1, addResponse);
    console.log(2, addResponse.body.data.toMap());
}
console.log("开始查询")
SearchUrl();

  • 结果示例

1 SearchByUrlResponse {
  headers: {
    date: 'Tue, 08 Jun 2021 08:18:34 GMT',
    'content-type': 'application/json;charset=utf-8',
    'content-length': '933',
    connection: 'keep-alive',
    'access-control-allow-origin': '*',
    'access-control-allow-methods': 'POST, GET, OPTIONS',
    'access-control-allow-headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signature',
    'access-control-max-age': '172800',
    'x-acs-request-id': 'D5AE998A-89AE-4753-9D8F-63586448BB1C'
  },
  body: SearchByUrlResponseBody {
    success: true,
    code: 0,
    data: SearchByUrlResponseBodyData { auctions: [Array] },
    requestId: 'D5AE998A-89AE-4753-9D8F-63586448BB1C',
    picInfo: SearchByUrlResponseBodyPicInfo {
      mainRegion: [SearchByUrlResponseBodyPicInfoMainRegion],
      multiRegion: [Array]
    }
  }
}
2 [
  SearchByUrlResponseBodyDataAuctions {
    result: SearchByUrlResponseBodyDataAuctionsResult {
      itemId: '557104149239',
      itemName: '皮肤科用药',
      pic: '//img.alicdn.com/xxxx.jpg',
      price: '7.8',
      promotionPrice: '7.8',
      userType: 1,
      provcity: '山东 枣庄',
      clickUrl: '//s.click.taobao.com/t?yyyyy'
    },
    rankScore: 0.7065
  }
]
3 [
  SearchByUrlResponseBodyPicInfoMultiRegion {
    region: '269,1212,253,717'
  }
]
3 SearchByUrlResponseBodyPicInfoMainRegion {
  region: '269,1212,253,717',
  multiCategoryId: [
    SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 88888888,
      score: 0.91438
    },
    SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 22,
      score: 0.01744
    },
    SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 7,
      score: 0.00839
    },
    SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
      categoryId: 8,
      score: 0.00767
    }
  ]
}

Process finished with exit code 0