本文介绍淘宝联盟服务Go SDK的使用方法及示例。
接口列表
接口名称 | 接口说明 |
SearchByPic | 用于向淘宝联盟版在线调用,根据图片查询。 |
SearchByUrl | 用于向淘宝联盟版在线调用,根据图片URL查询。 |
GetProductInfoByIds | 用于向淘宝联盟版在线调用,根据ID查询商品。 |
准备工作
在安装和使用阿里云SDK前,确保您已经注册阿里云账号并生成访问密钥(AccessKey)。详情请参见
使用Govendor工具执行以下命令,安装阿里云Go SDK:
go get github.com/alibabacloud-go/imagesearch-20210501
SearchByPic接口
代码示例
package main
import (
"fmt"
config "github.com/alibabacloud-go/darabonba-openapi/client"
imagesearch "github.com/alibabacloud-go/imagesearch-20210501/client"
util "github.com/alibabacloud-go/tea-utils/service"
"os"
)
// 创建AK/SK参考:https://help.aliyun.com/document_detail/116401.htm
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// 本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
var ak = os.Getenv("CC_AK_ENV")
var sk = os.Getenv("CC_SK_ENV")
var endpoint = "imagesearch.cn-shanghai.aliyuncs.com"
var regionId = "cn-shanghai"
var PIC_NAME = "C:\\image/9.jpg";
var PIC_URL = "https://image.example.com/pic.jpg";
func main() {
searchByPic();
}
func searchByPic(){
// 初始化 config
var config = new(config.Config).SetAccessKeyId(ak).
SetAccessKeySecret(sk).
SetType("access_key").
SetEndpoint(endpoint).
SetRegionId(regionId)
// 创建客户端
client, err := imagesearch.NewClient(config)
// 初始化 runtimeObject
var runtimeObject = new(util.RuntimeOptions)
b, err := os.Open(PIC_NAME)
request := new(imagesearch.SearchByPicAdvanceRequest).
SetPicContentObject(b).
// 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
// SetFields("Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle").
// 必填,PID
SetPid("mm_xxx_xxx_xxx").
// 选填,图片类目
//SetCategoryId(88888888).
// 选填,是否需要进行主体识别。默认true
//SetCrop(false).
// 选填,图片的主体区域,格式为x1,x2,y1,y2
//SetRegion("518,1524,398,1632").
// 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
SetStart(0).
// 选填,返回结果的数目。取值范围:1-20。默认值:10。
SetNum(1)
// 选填,渠道ID。用于淘宝联盟中的渠道区分。
//request.relationId = 1145;
resp, err := client.SearchByPicAdvance(request,runtimeObject)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(resp)
}
结果示例
{
"headers": {
"access-control-allow-headers": "X-Requested-With, X-Sequence, _aop_secret",
"access-control-allow-methods": "POST, GET, OPTIONS",
"access-control-allow-origin": "*",
"access-control-max-age": "172800",
"connection": "keep-alive",
"content-type": "application/json;charset=utf-8",
"date": "Wed, 04 Aug 2021 09:45:17 GMT",
"vary": "Accept-Encoding",
"x-acs-request-id": "xx-6EBF-xxx-8CF9-xxxx"
},
"body": {
"Success": true,
"Code": 0,
"Data": {
"Auctions": [
{
"Result": {
"Title": "免粘胶",
"PicUrl": "//456.jpg",
"ReservePrice": "9.9",
"ZkFinalPrice": "9.9",
"UserType": 0,
"Provcity": "广东广州",
"CouponTotalCount": "22",
"Url": "//s.click.taobao.com/t?xxx"
},
"RankScore": 0.8486
}
]
},
"RequestId": "xxx-6EBF-xx-8CF9-xxx",
"PicInfo": {
"MainRegion": {
"Region": "88,453,68,411",
"MultiCategoryId": [
{
"CategoryId": 88888888,
"Score": 0.72653
},
{
"CategoryId": 20,
"Score": 0.05694
},
{
"CategoryId": 9,
"Score": 0.05302
},
{
"CategoryId": 8,
"Score": 0.0404
}
]
},
"MultiRegion": [
{
"Region": "88,453,68,411"
},
{
"Region": "88,453,68,411"
},
{
"Region": "88,453,68,411"
}
]
}
}
}
SearchByUrl接口
代码示例
package main
import (
"fmt"
config "github.com/alibabacloud-go/darabonba-openapi/client"
imagesearch "github.com/alibabacloud-go/imagesearch-20210501/client"
util "github.com/alibabacloud-go/tea-utils/service"
"os"
)
// 创建AK/SK参考:https://help.aliyun.com/document_detail/116401.htm
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// 本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
var ak = os.Getenv("CC_AK_ENV")
var sk = os.Getenv("CC_SK_ENV")
var endpoint = "imagesearch.cn-shanghai.aliyuncs.com"
var regionId = "cn-shanghai"
var PIC_NAME = "C:\\image1/9.jpg";
var PIC_URL = "https://image.example.com/pic.jpg";
func main() {
searchByUrl();
}
func searchByUrl(){
// 初始化 config
var config = new(config.Config).SetAccessKeyId(ak).
SetAccessKeySecret(sk).
SetType("access_key").
SetEndpoint(endpoint).
SetRegionId(regionId)
// 创建客户端
client, err := imagesearch.NewClient(config)
// 初始化 runtimeObject
request := new(imagesearch.SearchByUrlRequest).
SetPicUrl(PIC_URL).
// 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
// SetFields("Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle").
// 必填,PID
SetPid("mm_xxx_xxx_xxx").
// 选填,图片类目
//SetCategoryId(88888888).
// 选填,是否需要进行主体识别。默认true
//SetCrop(false).
// 选填,图片的主体区域,格式为x1,x2,y1,y2
//SetRegion("518,1524,398,1632").
// 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
SetStart(0).
// 选填,返回结果的数目。取值范围:1-20。默认值:10。
SetNum(1)
// 选填,渠道ID。用于淘宝联盟中的渠道区分。
//request.relationId = 1145;
resp, err := client.SearchByUrl(request)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(resp)
}
结果示例
{
"headers": {
"access-control-allow-headers": "X-Requested-With, X-Sequence, _aop_secret",
"access-control-allow-methods": "POST, GET, OPTIONS",
"access-control-allow-origin": "*",
"access-control-max-age": "172800",
"connection": "keep-alive",
"content-type": "application/json;charset=utf-8",
"date": "Wed, 04 Aug 2021 09:45:18 GMT",
"vary": "Accept-Encoding",
"x-acs-request-id": "xxx-D980-xxx-86BA-xxx"
},
"body": {
"Success": true,
"Code": 0,
"Data": {
"Auctions": [
{
"Result": {
"Title": "餐刀勺子",
"PicUrl": "//283.jpg",
"ReservePrice": "1.7",
"ZkFinalPrice": "1.7",
"UserType": 0,
"Provcity": "上海",
"CouponTotalCount": "78",
"CouponShareUrl": "xxx",
"Url": "//s.click.taobao.com/t?xxxx"
},
"RankScore": 0.7991
}
]
},
"RequestId": "xxx-D980-xxx-86BA-xxxx",
"PicInfo": {
"MainRegion": {
"Region": "152,274,66,708",
"MultiCategoryId": [
{
"CategoryId": 88888888,
"Score": 0.80207
},
{
"CategoryId": 7,
"Score": 0.04759
},
{
"CategoryId": 20,
"Score": 0.04636
},
{
"CategoryId": 9,
"Score": 0.0173
}
]
},
"MultiRegion": [
{
"Region": "152,274,66,708"
},
{
"Region": "152,274,66,708"
},
{
"Region": "394,499,83,730"
},
{
"Region": "555,657,42,758"
},
{
"Region": "551,655,47,759"
}
]
}
}
}
GetProductInfoByIds接口
代码示例
package main
import (
"fmt"
config "github.com/alibabacloud-go/darabonba-openapi/client"
imagesearch "github.com/alibabacloud-go/imagesearch-20210501/client"
)
// 创建AK/SK参考:https://help.aliyun.com/document_detail/116401.htm
// 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
// 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
// 本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
var ak = os.Getenv("CC_AK_ENV")
var sk = os.Getenv("CC_SK_ENV")
var endpoint = "imagesearch.cn-shanghai.aliyuncs.com"
var regionId = "cn-shanghai"
func main() {
getProductInfo()
}
func getProductInfo() {
// 初始化 config
var config = new(config.Config).SetAccessKeyId(ak).
SetAccessKeySecret(sk).
SetType("access_key").
SetEndpoint(endpoint).
SetRegionId(regionId)
// 创建客户端
client, err := imagesearch.NewClient(config)
request := new(imagesearch.GetProductInfoByIdsRequest).
// 必填,PID
SetPid("xxxxxxxx").
// 必填商品ID串,用','分割,最大40个。
SetItemIds("xxxx").
// 需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
SetFields("xxxx")
resp, err := client.GetProductInfoByIds(request)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(resp)
}
结果示例
{
"body": {
"Code": 0,
"Data": {
"Auctions": [
{
"Result": {
"CommissionRate": "300",
"MaxCommission": {},
"PicUrl": "xxxxxx.jpg",
"ReservePrice": "35",
"Title": "xxxxxx",
"Url": "xxxxxxxx",
"ZkFinalPrice": "28"
}
}
]
},
"RequestId": "xxxx-xxx-xxx-xxx-xxxxxxx",
"Success": true
}
}
文档内容是否对您有帮助?