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

接口列表

接口名称

接口说明

SearchByPic

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

SearchByUrl

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

GetProductInfoByIds

用于向淘宝联盟版在线调用,根据ID查询商品。

准备工作

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

  1. 在解决方案资源管理器面板中,右键单击您的项目,选择管理NuGet程序包菜单。

  2. 在打开的NuGet管理面板中单击浏览选项卡输入AlibabaCloud.SDK.ImageSearch20210501 。

  3. 点击安装。

SearchByPic接口

  • 代码示例

using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.ImageSearch20210501.Models;
using AlibabaCloud.SDK.ImageSearch20210501;
using System;
using System.IO;
using AlibabaCloud.TeaUtil.Models;

namespace netdemoimagesearch_20210501
{
    class Program
    {
        private static String PIC_NAME = "C:\\xxx/9.jpg";

        private static String PIC_URL = "https://xxxx.oss-cn-shanghai.aliyuncs.com/m.jpg";
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            searchPic();
        }

        public static Client getClient()
        {
            Config config = new Config();
            //阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            //强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            //本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
            IDictionary environmentVariables = Environment.GetEnvironmentVariables();
            foreach (DictionaryEntry de in environmentVariables)
            {
                if (de.Key.Equals("CC_AK_ENV")) {
                    config.AccessKeyId = (string)de.Value;
                }
                if (de.Key.Equals("CC_SK_ENV"))
                {
                    config.AccessKeySecret = (string)de.Value;
                }
            }
            config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
            config.RegionId = "cn-shanghai";
            Client client = new Client(config);
            return client;
        }

        public static void searchPic()
        {


            SearchByPicAdvanceRequest request = new SearchByPicAdvanceRequest();
            FileStream fs = File.OpenRead(PIC_NAME);
            request.PicContentObject = fs;
            // 必填,PID
            request.Pid = "mm_xx_xx_xxx";
            // 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
            // request.Fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
            // 选填,图片类目
            //request.CategoryId = 88888888;
            // 选填,是否需要进行主体识别。默认true
            //request.Crop = false;
            // 选填,图片的主体区域,格式为x1,x2,y1,y2
            //request.Region = "518,1524,398,1632";
            // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
            request.Start = 0;
            // 选填,返回结果的数目。取值范围:1-20。默认值:10。
            request.Num = 2;
            // 选填,渠道ID。用于淘宝联盟中的渠道区分。
            //request.relationId = 1145;
            RuntimeOptions runtimeObject = new RuntimeOptions();
            SearchByPicResponse response = getClient().SearchByPicAdvance(request,runtimeObject);

            Console.WriteLine("searchPic " + Newtonsoft.Json.JsonConvert.SerializeObject(response.Body.Data));

        }
    }
}

  • 结果示例

{
  "Auctions": [
    {
      "Result": {
        "Title": "免粘胶",
        "PicUrl": "//456.jpg",
        "ReservePrice": "9.9",
        "PromotionPrice": null,
        "ZkFinalPrice": "9.9",
        "UserType": 0,
        "Provcity": "广东广州",
        "Nick": null,
        "SellerId": null,
        "Volume": null,
        "LevelOneCategoryName": null,
        "CategoryName": null,
        "CouponTotalCount": "",
        "CouponRemainCount": null,
        "CouponStartTime": null,
        "CouponEndTime": null,
        "CouponStartFee": null,
        "CouponAmount": null,
        "CouponInfo": null,
        "CommissionRate": null,
        "CouponShareUrl": null,
        "Url": "//s.click.taobao.com/t?xxx",
        "ShopTitle": null
      },
      "RankScore": 0.8486
    }
  ]
}

SearchByUrl接口

  • 代码示例

using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.ImageSearch20210501.Models;
using AlibabaCloud.SDK.ImageSearch20210501;
using System;
using System.IO;
using AlibabaCloud.TeaUtil.Models;

namespace netdemoimagesearch_20210501
{
    class Program
    {
        private static String PIC_NAME = "C:\\xx/9.jpg";

        private static String PIC_URL = "https://xx.oss-cn-shanghai.aliyuncs.com/m.jpg";
        static void Main(string[] args)
        {
            searchUrl();
        }

        public static Client getClient()
        {
            Config config = new Config();
            //阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            //强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            //本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
            IDictionary environmentVariables = Environment.GetEnvironmentVariables();
            foreach (DictionaryEntry de in environmentVariables)
            {
                if (de.Key.Equals("CC_AK_ENV")) {
                    config.AccessKeyId = (string)de.Value;
                }
                if (de.Key.Equals("CC_SK_ENV"))
                {
                    config.AccessKeySecret = (string)de.Value;
                }
            }
            config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
            config.RegionId = "cn-shanghai";
            Client client = new Client(config);
            return client;
        }


        public static void searchUrl()
        {


            SearchByUrlRequest request = new SearchByUrlRequest();
            request.PicUrl = PIC_URL;
            // 必填,PID
            request.Pid = "mm_xx_xx_xxx";
            // 选填,需要返回的字段list。不同的字段用逗号分割。默认 PicUrl,ReservePrice,Title,Url,ZkFinalPrice
            // request.Fields = "Title,PicUrl,ReservePrice,ZkFinalPrice,UserType,Provcity,Nick,SellerId,Volume,LevelOneCategoryName,CategoryName,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,Url,ShopTitle";
            // 选填,图片类目
            //request.CategoryId = 88888888;
            // 选填,是否需要进行主体识别。默认true
            //request.Crop = false;
            // 选填,图片的主体区域,格式为x1,x2,y1,y2
            //request.Region = "518,1524,398,1632";
            // 选填,返回结果的起始位置。取值范围:0-499。默认值:0。
            request.Start = 0;
            // 选填,返回结果的数目。取值范围:1-20。默认值:10。
            request.Num = 2;
            // 选填,渠道ID。用于淘宝联盟中的渠道区分。
            //request.relationId = 1145;
            RuntimeOptions runtimeObject = new RuntimeOptions();
            SearchByUrlResponse response = getClient().SearchByUrl(request);

            Console.WriteLine("searchUrl " + Newtonsoft.Json.JsonConvert.SerializeObject(response.Body.Data));

        }
    }
}

  • 结果示例

{
  "Auctions": [
    {
      "Result": {
        "Title": "达林用药",
        "PicUrl": "//img.pic.jpg",
        "ReservePrice": "7.8",
        "ZkFinalPrice": "7.8",
        "UserType": 1,
        "Provcity": "山东枣庄",
        "Nick": null,
        "SellerId": null,
        "Volume": null,
        "LevelOneCategoryName": null,
        "CategoryName": null,
        "CouponTotalCount": "",
        "CouponRemainCount": null,
        "CouponStartTime": null,
        "CouponEndTime": null,
        "CouponStartFee": null,
        "CouponAmount": null,
        "CouponInfo": null,
        "CommissionRate": null,
        "CouponShareUrl": null,
        "Url": "//s.click.taobao.com/t?xxx",
        "ShopTitle": null
      },
      "RankScore": 0.71
    }
  ]
}

GetProductInfoByIds接口

  • 代码示例

using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.ImageSearch20210501.Models;
using AlibabaCloud.SDK.ImageSearch20210501;
using System;
using AlibabaCloud.TeaUtil.Models;

namespace netdemoimagesearch_20210501
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            getProduct();
        }
        public static Client getClient()
        {
            Config config = new Config();
            //阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            //强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            //本示例以将AccessKey ID和AccessKey Secret保存在环境变量为例说明。您也可以根据业务需要,保存到配置文件里。
            IDictionary environmentVariables = Environment.GetEnvironmentVariables();
            foreach (DictionaryEntry de in environmentVariables)
            {
                if (de.Key.Equals("CC_AK_ENV")) {
                    config.AccessKeyId = (string)de.Value;
                }
                if (de.Key.Equals("CC_SK_ENV"))
                {
                    config.AccessKeySecret = (string)de.Value;
                }
            }
            config.Endpoint = "imagesearch.cn-shanghai.aliyuncs.com";
            config.RegionId = "cn-shanghai";
            Client client = new Client(config);
            return client;
        }

        public static void getProduct()
        {
            GetProductInfoByIdsRequest request = new GetProductInfoByIdsRequest();
            // 必填,PID
            request.Pid = "mm_123123_123123_123123123";
            // 必填商品ID串,用','分割,最大40个。
            request.ItemIds = "587928509576";
            // 需要返回的字段list.不同的字段用逗号分割.默认PicUrl,ReservePrice,Title,Url,ZkFinalPrice
            request.Fields = "CommissionRate";
            RuntimeOptions runtimeObject = new RuntimeOptions();
            GetProductInfoByIdsResponse response = getClient().GetProductInfoByIds(request);
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(response.Body));
        }
    }
}

  • 结果示例

 {
        "code": 0,
        "data": {
            "auctions": [
                {
                    "result": {
                        "commissionRate": "300",
                        "maxCommission": {
                            
                        },
                        "picUrl": "asdcasxz.jxx",
                        "reservePrice": "35",
                        "title": "ascascasc",
                        "url": "xxxx",
                        "zkFinalPrice": "28"
                    }
                }
            ]
        },
        "requestId": "xxx-D936-xxx-9823-xx",
        "success": true
    }