本文介绍淘宝联盟服务Node.js SDK的使用方法及示例。
接口列表
接口名称 | 接口说明 |
SearchByPic | 用于向淘宝联盟版在线调用,根据图片查询。 |
SearchByUrl | 用于向淘宝联盟版在线调用,根据图片URL查询。 |
GetProductInfoByIds | 用于向淘宝联盟版在线调用,根据ID查询商品。 |
准备工作
在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见
安装Node.js SDK
npm install @alicloud/imagesearch20210501@1.2.1
SearchByPic接口
代码示例
const oss = require('@alicloud/oss-util');
const fs = require('fs');
const imagesearch = require('@alicloud/imagesearch20210501')
const client = new imagesearch.default({
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// 本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
accessKeyId: process.env.CC_AK_ENV,
accessKeySecret: process.env.CC_SK_ENV,
type: "access_key",
endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
regionId: "cn-shanghai",
protocol: 'http'
});
var PIC_NAME = "C:\\xx/9.jpg";
var PIC_URL = "https://xx.oss-cn-shanghai.aliyuncs.com/m.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,
// 必填,PID
pid: "mm_xxx_xxx_xxx",
// 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
//fields: "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle",
// 选填,图片类目
//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.auctions);
console.log(3, addResponse.body.picInfo.multiRegion);
console.log(3, addResponse.body.picInfo.mainRegion);
}
console.log("开始查询")
SearchPic();
结果示例
1 SearchByPicResponse {
headers: {
date: 'Wed, 04 Aug 2021 08:47:11 GMT',
'content-type': 'application/json;charset=utf-8',
'content-length': '1155',
connection: 'keep-alive',
vary: 'Accept-Encoding',
'access-control-allow-origin': '*',
'access-control-allow-methods': 'POST, GET, OPTIONS',
'access-control-allow-headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx',
'access-control-max-age': '172800',
'x-acs-request-id': 'xxx-9D6E-xxx-BEFF-xxx'
},
body: SearchByPicResponseBody {
success: true,
code: 0,
data: SearchByPicResponseBodyData { auctions: [Array] },
requestId: 'xxx-9D6E-xxx-BEFF-xxx',
picInfo: SearchByPicResponseBodyPicInfo {
mainRegion: [SearchByPicResponseBodyPicInfoMainRegion],
multiRegion: [Array]
}
}
}
2 [
SearchByPicResponseBodyDataAuctions {
result: SearchByPicResponseBodyDataAuctionsResult {
title: 'c免粘胶',
picUrl: '//img.alicdn.com/456.jpg',
reservePrice: '9.9',
zkFinalPrice: '9.9',
userType: 0,
provcity: '广东广州',
couponTotalCount: '',
url: '//s.click.taobao.com/t?xxx'
},
rankScore: 0.8486
}
]
3 [
SearchByPicResponseBodyPicInfoMultiRegion { region: '88,453,68,411' },
SearchByPicResponseBodyPicInfoMultiRegion { region: '88,453,68,411' },
SearchByPicResponseBodyPicInfoMultiRegion { region: '88,453,68,411' }
]
3 SearchByPicResponseBodyPicInfoMainRegion {
region: '88,453,68,411',
multiCategoryId: [
SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 88888888,
score: 0.72653
},
SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 20,
score: 0.05694
},
SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 9,
score: 0.05302
},
SearchByPicResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 8,
score: 0.0404
}
]
}
Process finished with exit code 0
SearchByUrl接口
代码示例
const oss = require('@alicloud/oss-util');
const fs = require('fs');
const imagesearch = require('@alicloud/imagesearch20210501')
const client = new imagesearch.default({
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// 本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
accessKeyId: process.env.CC_AK_ENV,
accessKeySecret: process.env.CC_SK_ENV,
type: "access_key",
endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
regionId: "cn-shanghai",
protocol: 'http'
});
var PIC_NAME = "C:\\xxx/9.jpg";
var PIC_URL = "https://xx.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,
// 必填,PID
pid: "mm_xx_xx_xx",
// 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
// fields: "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle",
// 选填,图片类目
//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.auctions);
console.log(3, addResponse.body.picInfo.multiRegion);
console.log(3, addResponse.body.picInfo.mainRegion);
}
SearchUrl();
结果示例
开始查询
1 SearchByUrlResponse {
headers: {
date: 'Wed, 04 Aug 2021 09:11:48 GMT',
'content-type': 'application/json;charset=utf-8',
'content-length': '1133',
connection: 'keep-alive',
vary: 'Accept-Encoding',
'access-control-allow-origin': '*',
'access-control-allow-methods': 'POST, GET, OPTIONS',
'access-control-allow-headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx',
'access-control-max-age': '172800',
'x-acs-request-id': 'xxx-88F6-xxx-8107-xxx'
},
body: SearchByUrlResponseBody {
success: true,
code: 0,
data: SearchByUrlResponseBodyData { auctions: [Array] },
requestId: 'xxx-88F6-xxx-8107-xxxx',
picInfo: SearchByUrlResponseBodyPicInfo {
mainRegion: [SearchByUrlResponseBodyPicInfoMainRegion],
multiRegion: [Array]
}
}
}
2 [
SearchByUrlResponseBodyDataAuctions {
result: SearchByUrlResponseBodyDataAuctionsResult {
title: '达林用药',
picUrl: '//img.alicdn.com/pic.jpg',
reservePrice: '7.8',
zkFinalPrice: '7.8',
userType: 1,
provcity: '山东枣庄',
couponTotalCount: '',
url: '//s.click.taobao.com/t?xxxxx'
},
rankScore: 0.71
}
]
3 [
SearchByUrlResponseBodyPicInfoMultiRegion {
region: '263,1238,259,715'
},
SearchByUrlResponseBodyPicInfoMultiRegion {
region: '263,1238,259,715'
}
]
3 SearchByUrlResponseBodyPicInfoMainRegion {
region: '263,1238,259,715',
multiCategoryId: [
SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 88888888,
score: 0.89114
},
SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 22,
score: 0.02755
},
SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 8,
score: 0.01099
},
SearchByUrlResponseBodyPicInfoMainRegionMultiCategoryId {
categoryId: 6,
score: 0.01038
}
]
}
GetProductInfoByIds接口
代码示例
const imagesearch = require('@alicloud/imagesearch20210501')
const client = new imagesearch.default({
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// 本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
accessKeyId: process.env.CC_AK_ENV,
accessKeySecret: process.env.CC_SK_ENV,
type: "access_key",
endpoint: "imagesearch.cn-shanghai.aliyuncs.com",
regionId: "cn-shanghai",
protocol: 'http'
});
// 根据ID查询
async function getProductInfo() {
const request = new imagesearch.GetProductInfoByIdsRequest({
// 必填,PID
pid: "xxxxx",
// 必填商品ID串,用','分割,最大40个。
itemIds: "asdasdw",
// 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
fields: "xxxx"
});
const addResponse = await client.getProductInfoByIds(request);
console.log(1, addResponse);
console.log(2, addResponse.body.data.auctions);
}
console.log("开始查询")
getProductInfo();
结果示例
开始查询
1 GetProductInfoByIdsResponse {
headers: {
date: 'Tue, 09 Aug 2022 06:26:18 GMT',
'content-type': 'application/json;charset=utf-8',
'content-length': '751',
connection: 'keep-alive',
'access-control-allow-origin': '*',
'access-control-allow-methods': 'POST, GET, OPTIONS, PUT, DELETE',
'access-control-allow-headers': 'X-Requested-With, X-Sequence, _aop_secret, _aop_signaxxxx, x-acs-action, x-acs-version, x-acs-date, Content-Type',
'access-control-max-age': '172800',
'x-acs-request-id': 'xxxx-xxx-xxxx-xxx-xxxxx',
'x-acs-trace-id': '22af75e793b81c3faf850a33441d5360'
},
body: GetProductInfoByIdsResponseBody {
code: 0,
data: GetProductInfoByIdsResponseBodyData { auctions: [Array] },
requestId: 'xxxx-xxx-5743-xxx-xxxx',
success: true
}
}
2 [
GetProductInfoByIdsResponseBodyDataAuctions {
result: GetProductInfoByIdsResponseBodyDataAuctionsResult {
commissionRate: '300',
maxCommission: GetProductInfoByIdsResponseBodyDataAuctionsResultMaxCommission {},
picUrl: 'xxxxx.jpg',
reservePrice: '35',
title: 'xxxxx',
url: 'xxxxxx',
zkFinalPrice: '28'
}
}
]
文档内容是否对您有帮助?