golang亚马逊产品广告API 5.0客户端插件库gopaapi5的使用

golang亚马逊产品广告API 5.0客户端插件库gopaapi5的使用

gopaapi5是一个用于访问亚马逊产品广告API 5.0的Go客户端库。

安装

使用以下命令安装包:

go get -u github.com/utekaravinash/gopaapi5

使用示例

GetBrowseNodes操作示例

以下是一个获取浏览节点(GetBrowseNodes)的完整示例:

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/utekaravinash/gopaapi5"
	"github.com/utekaravinash/gopaapi5/api"
)

const tmpl = `Id: %s
Name: %s
-----------------------------
`

func main() {
	// 从环境变量获取访问密钥、密钥和关联标签
	accessKey := os.Getenv("PA_ACCESS_KEY")
	secretKey := os.Getenv("PA_SECRET_KEY")
	associateTag := os.Getenv("PA_ASSOCIATE_TAG")

	// 初始化gopaapi5客户端
	client, err := gopaapi5.NewClient(accessKey, secretKey, associateTag, api.UnitedStates)
	if err != nil {
		panic(err)
	}

	// 构建GetBrowseNodes操作的请求参数
	params := api.GetBrowseNodesParams{
		BrowseNodeIds: []string{
			"6960520011",
			"281407",
		},
		Resources: []api.Resource{
			api.BrowseNodesAncestor,
			api.BrowseNodesChildren,
		},
		LanguagesOfPreference: []api.Language{api.EnglishUnitedStates},
	}

	// 调用GetBrowseNodes操作
	response, err := client.GetBrowseNodes(context.Background(), &params)
	if err != nil {
		panic(err)
	}

	// 遍历响应中的浏览节点
	for _, node := range response.BrowseNodesResult.BrowseNodes {
		fmt.Printf(tmpl, node.Id, node.DisplayName)
	}
}

支持的API操作

该客户端库支持以下亚马逊产品广告API 5.0操作:

  • GetBrowseNodes - 获取浏览节点信息
  • GetItems - 获取商品信息
  • GetVariations - 获取商品变体信息
  • SearchItems - 搜索商品

注意:可以使用Client结构体的SetHttpClient()方法来设置自定义HTTP客户端。

作者

Avinash Utekar

许可证

BSD 3-Clause


更多关于golang亚马逊产品广告API 5.0客户端插件库gopaapi5的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang亚马逊产品广告API 5.0客户端插件库gopaapi5的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用gopaapi5库访问亚马逊产品广告API 5.0

gopaapi5是一个用于访问亚马逊产品广告API 5.0的Golang客户端库。下面我将介绍如何使用这个库来获取亚马逊产品数据。

安装

首先,使用go get安装gopaapi5库:

go get github.com/utekaravinash/gopaapi5

基本使用

1. 初始化客户端

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/utekaravinash/gopaapi5"
	"github.com/utekaravinash/gopaapi5/api"
)

func main() {
	// 从环境变量获取凭证
	accessKey := os.Getenv("AWS_ACCESS_KEY")
	secretKey := os.Getenv("AWS_SECRET_KEY")
	partnerTag := os.Getenv("AMAZON_PARTNER_TAG") // 你的亚马逊联盟ID

	// 创建客户端配置
	client, err := gopaapi5.NewClient(
		gopaapi5.WithAccessKey(accessKey),
		gopaapi5.WithSecretKey(secretKey),
		gopaapi5.WithPartnerTag(partnerTag),
		gopaapi5.WithMarketplace(api.UnitedStates), // 选择市场
	)
	if err != nil {
		log.Fatalf("创建客户端失败: %v", err)
	}

2. 获取商品信息

	// 准备请求参数
	itemIDs := []string{"B08N5KWB9H"} // 亚马逊ASIN列表
	resources := []api.Resource{
		api.ImagesPrimaryLarge,
		api.ItemInfoTitle,
		api.OffersListingsPrice,
	}

	// 创建GetItems请求
	getItemsRequest := api.GetItemsRequest{
		ItemIDs:   itemIDs,
		Resources: resources,
	}

	// 发送请求
	response, err := client.GetItems(getItemsRequest)
	if err != nil {
		log.Fatalf("获取商品信息失败: %v", err)
	}

	// 处理响应
	if len(response.ItemsResult.Items) > 0 {
		item := response.ItemsResult.Items[0]
		fmt.Printf("商品标题: %s\n", item.ItemInfo.Title.DisplayValue)
		fmt.Printf("价格: %s\n", item.Offers.Listings[0].Price.DisplayAmount)
		fmt.Printf("主图URL: %s\n", item.Images.Primary.Large.URL)
	} else {
		fmt.Println("未找到商品信息")
	}

3. 搜索商品

	// 创建搜索请求
	searchRequest := api.SearchItemsRequest{
		Keywords:  "wireless headphones",
		Resources: resources,
		SearchIndex: api.Electronics, // 搜索类别
		ItemCount:  5,               // 返回结果数量
	}

	// 发送搜索请求
	searchResponse, err := client.SearchItems(searchRequest)
	if err != nil {
		log.Fatalf("搜索商品失败: %v", err)
	}

	// 处理搜索结果
	fmt.Println("\n搜索结果:")
	for _, item := range searchResponse.SearchResult.Items {
		fmt.Printf("- %s (ASIN: %s)\n", item.ItemInfo.Title.DisplayValue, item.ASIN)
	}

高级功能

1. 使用分页

	// 带分页的搜索
	pagedRequest := api.SearchItemsRequest{
		Keywords:    "smart watch",
		Resources:   resources,
		SearchIndex: api.Electronics,
		ItemCount:   10,
		ItemPage:    2, // 第二页
	}

	pagedResponse, err := client.SearchItems(pagedRequest)
	if err != nil {
		log.Fatalf("分页搜索失败: %v", err)
	}

2. 获取商品变体

	// 获取商品变体信息
	variationRequest := api.GetVariationsRequest{
		ASIN:      "B08N5KWB9H", // 父ASIN
		Resources: resources,
	}

	variationResponse, err := client.GetVariations(variationRequest)
	if err != nil {
		log.Fatalf("获取商品变体失败: %v", err)
	}

	fmt.Println("\n商品变体:")
	for _, item := range variationResponse.VariationsResult.Items {
		fmt.Printf("- %s (ASIN: %s)\n", item.ItemInfo.Title.DisplayValue, item.ASIN)
	}

错误处理

	// 检查API错误
	if response.Errors != nil {
		for _, apiError := range response.Errors {
			fmt.Printf("API错误: %s - %s\n", apiError.Code, apiError.Message)
		}
	}

	// 检查HTTP错误
	if err != nil {
		if apiErr, ok := err.(*api.Error); ok {
			fmt.Printf("API错误: %d - %s\n", apiErr.StatusCode, apiErr.Message)
		} else {
			fmt.Printf("请求错误: %v\n", err)
		}
	}

完整示例

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/utekaravinash/gopaapi5"
	"github.com/utekaravinash/gopaapi5/api"
)

func main() {
	// 初始化客户端
	client, err := gopaapi5.NewClient(
		gopaapi5.WithAccessKey(os.Getenv("AWS_ACCESS_KEY")),
		gopaapi5.WithSecretKey(os.Getenv("AWS_SECRET_KEY")),
		gopaapi5.WithPartnerTag(os.Getenv("AMAZON_PARTNER_TAG")),
		gopaapi5.WithMarketplace(api.UnitedStates),
	)
	if err != nil {
		log.Fatal(err)
	}

	// 搜索商品
	searchResponse, err := client.SearchItems(api.SearchItemsRequest{
		Keywords:    "wireless earbuds",
		Resources: []api.Resource{
			api.ItemInfoTitle,
			api.OffersListingsPrice,
			api.ImagesPrimaryLarge,
		},
		SearchIndex: api.Electronics,
		ItemCount:  3,
	})
	if err != nil {
		log.Fatal(err)
	}

	// 显示结果
	fmt.Println("无线耳机搜索结果:")
	for i, item := range searchResponse.SearchResult.Items {
		fmt.Printf("%d. %s\n", i+1, item.ItemInfo.Title.DisplayValue)
		fmt.Printf("   价格: %s\n", item.Offers.Listings[0].Price.DisplayAmount)
		fmt.Printf("   图片: %s\n\n", item.Images.Primary.Large.URL)
	}
}

注意事项

  1. 确保已注册亚马逊联盟计划并获取有效的Partner Tag
  2. 妥善保管AWS访问密钥和密钥
  3. 遵守亚马逊API的使用条款和限制
  4. 考虑实现缓存机制以减少API调用
  5. 处理API速率限制和错误响应

gopaapi5库提供了对亚马逊产品广告API 5.0的完整访问能力,你可以根据需要扩展上述示例来实现更复杂的功能。

回到顶部