全部产品

Go SDK

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

接口列表

接口名称

接口说明

SearchByPic

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

SearchByUrl

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

准备工作

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

    创建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"
)
var ak = "XXXXXX"
var sk = "XXXXXXX"
var endpoint = "imagesearch.cn-shanghai.aliyuncs.com"
var regionId = "cn-shanghai"
var PIC_NAME = "C:\\XXXXXX/9.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。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
  SetFields("Provcity,UserType,CouponTotalCount,ItemId").
  // 图片类目
  //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。用于淘宝联盟中的渠道区分。
  //SetRelationId(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, _aop_signature",
      "access-control-allow-methods": "POST, GET, OPTIONS",
      "access-control-allow-origin": "*",
      "access-control-max-age": "172800",
      "connection": "keep-alive",
      "content-length": "932",
      "content-type": "application/json;charset=utf-8",
      "date": "Tue, 08 Jun 2021 09:37:17 GMT",
      "x-acs-request-id": "09F33A59-FD20-46CE-9688-B9D384A61BB4"
   },
   "body": {
      "Success": true,
      "Code": 0,
      "Data": {
         "Auctions": [
            {
               "Result": {
                  "ItemId": "558112896177",
                  "ItemName": "PPR三通接头配件",
                  "Pic": "//img.alicdn.com/i1/xxxxxx.jpg",
                  "Price": "0.12",
                  "PromotionPrice": "0.12",
                  "UserType": 0,
                  "Provcity": "山东 临沂",
                  "ClickUrl": "//s.click.taobao.com/t?yyyyyy"
               },
               "RankScore": 0.8301
            }
         ]
      },
      "RequestId": "09F33A59-FD20-46CE-9688-B9D384A61BB4",
      "PicInfo": {
         "MainRegion": {
            "Region": "55,456,65,415",
            "MultiCategoryId": [
               {
                  "CategoryId": 88888888,
                  "Score": 0.7559
               },
               {
                  "CategoryId": 20,
                  "Score": 0.05169
               },
               {
                  "CategoryId": 8,
                  "Score": 0.04217
               },
               {
                  "CategoryId": 9,
                  "Score": 0.03845
               }
            ]
         },
         "MultiRegion": [
            {
               "Region": "55,456,65,415"
            }
         ]
      }
   }
}

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"
)
var ak = "xxxx"
var sk = "xxxxxx"
var endpoint = "imagesearch.cn-shanghai.aliyuncs.com"
var regionId = "cn-shanghai"

var PIC_URL = "https://ossname.oss-cn-shanghai.aliyuncs.com/m.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。不同的字段用逗号分割。默认ClickUrl,ItemName,Pic,Price,PromotionPrice字段必返回
  SetFields("Provcity,UserType,CouponTotalCount,ItemId").
  // 图片类目
  //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。用于淘宝联盟中的渠道区分。

  //SetRelationId(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, _aop_signature",
      "access-control-allow-methods": "POST, GET, OPTIONS",
      "access-control-allow-origin": "*",
      "access-control-max-age": "172800",
      "connection": "keep-alive",
      "content-length": "933",
      "content-type": "application/json;charset=utf-8",
      "date": "Tue, 08 Jun 2021 09:40:19 GMT",
      "x-acs-request-id": "499F28E7-6489-45C7-A13E-D2B08E804DA1"
   },
   "body": {
      "Success": true,
      "Code": 0,
      "Data": {
         "Auctions": [
            {
               "Result": {
                  "ItemId": "557104149239",
                  "ItemName": "皮肤科用药",
                  "Pic": "//img.alicdn.com/i2/xxxxx.jpg",
                  "Price": "7.8",
                  "PromotionPrice": "7.8",
                  "UserType": 1,
                  "Provcity": "山东 枣庄",
                  "ClickUrl": "//s.click.taobao.com/t?yyyyy"
               },
               "RankScore": 0.7065
            }
         ]
      },
      "RequestId": "499F28E7-6489-45C7-A13E-D2B08E804DA1",
      "PicInfo": {
         "MainRegion": {
            "Region": "269,1212,253,717",
            "MultiCategoryId": [
               {
                  "CategoryId": 88888888,
                  "Score": 0.91438
               },
               {
                  "CategoryId": 22,
                  "Score": 0.01744
               },
               {
                  "CategoryId": 7,
                  "Score": 0.00839
               },
               {
                  "CategoryId": 8,
                  "Score": 0.00767
               }
            ]
         },
         "MultiRegion": [
            {
               "Region": "269,1212,253,717"
            }
         ]
      }
   }
}