本指南假设您已全面了解我们的入门指南,并且已设置为发出授权的请求。
设置完所有内容后,您可以向 Google Content API for Shopping 发送请求。以下代码示例演示了如何发送一些简单的请求:
- 创建商品
- 获取商品列表。
- 从列表中检索特定商品。
- 更新商品的价格。
如需查看完整的方法列表,请参阅参考文档。
有关详情,请参阅最佳做法页面。此页面介绍了如何最有效地利用 API,还解释了为什么建议发出批处理请求。
创建商品
您可以在商品 Feed 规范中找到商品可以拥有的所有属性的全部详情。要创建商品,请使用以下代码:
协议
POST /content/v2/YOUR_MERCHANT_ID/products { "offerId": "book123", "title": "A Tale of Two Cities", "description": "A classic novel about the French Revolution", "link": "http://my-book-shop.com/tale-of-two-cities.html", "imageLink": "http://my-book-shop.com/tale-of-two-cities.jpg", "contentLanguage": "en", "targetCountry": "GB", "channel": "online", "availability": "in stock", "condition": "new", "googleProductCategory": "Media > Books", "gtin": "9780007350896", "price": { "value": "2.50", "currency": "GBP" }, "shipping": [{ "country": "GB", "service": "Standard shipping", "price": { "value": "0.99", "currency": "GBP" } }], "shippingWeight": { "value": "200", "unit": "grams" } }
Java
Product product = new Product(); product.setOfferId("book123"); product.setTitle("A Tale of Two Cities"); product.setDescription("A classic novel about the French Revolution"); product.setLink("http://my-book-shop.com/tale-of-two-cities.html"); product.setImageLink("http://my-book-shop.com/tale-of-two-cities.jpg"); product.setContentLanguage("en"); product.setTargetCountry("GB"); product.setChannel("online"); product.setAvailability("in stock"); product.setCondition("new"); product.setGoogleProductCategory("Media > Books"); product.setGtin("9780007350896"); Price price = new Price(); price.setValue("2.50"); price.setCurrency("GBP"); product.setPrice(price); Price shippingPrice = new Price(); shippingPrice.setValue("0.99"); shippingPrice.setCurrency("GBP"); ProductShipping shipping = new ProductShipping(); shipping.setPrice(shippingPrice); shipping.setCountry("GB"); shipping.setService("Standard shipping"); ArrayListshippingList = new ArrayList (); shippingList.add(shipping); product.setShipping(shippingList); Product result = service.products().insert(merchantId, product).execute();
PHP
$product = new Google_Service_ShoppingContent_Product(); $product->setOfferId('book123'); $product->setTitle('A Tale of Two Cities'); $product->setDescription('A classic novel about the French Revolution'); $product->setLink('http://my-book-shop.com/tale-of-two-cities.html'); $product->setImageLink('http://my-book-shop.com/tale-of-two-cities.jpg'); $product->setContentLanguage('en'); $product->setTargetCountry('GB'); $product->setChannel('online'); $product->setAvailability('in stock'); $product->setCondition('new'); $product->setGoogleProductCategory('Media > Books'); $product->setGtin('9780007350896'); $price = new Google_Service_ShoppingContent_Price(); $price->setValue('2.50'); $price->setCurrency('GBP'); $shipping_price = new Google_Service_ShoppingContent_Price(); $shipping_price->setValue('0.99'); $shipping_price->setCurrency('GBP'); $shipping = new Google_Service_ShoppingContent_ProductShipping(); $shipping->setPrice($shipping_price); $shipping->setCountry('GB'); $shipping->setService('Standard shipping'); $shipping_weight = new Google_Service_ShoppingContent_ProductShippingWeight(); $shipping_weight->setValue(200); $shipping_weight->setUnit('grams'); $product->setPrice($price); $product->setShipping(array($shipping)); $product->setShippingWeight($shipping_weight); $result = $service->products->insert($merchant_id, $product);
获取商品列表
要获取商品列表,请使用以下代码:
协议
GET /content/v2/YOUR_MERCHANT_ID/products
Java
List productsList = service.products().list(merchantId); ProductsListResponse page = productsList.execute(); while ((page.getResources() != null) && !page.getResources().isEmpty()) { for (Product product : page.getResources()) { System.out.printf("%s %s%n", product.getId(), product.getTitle()); } if (page.getNextPageToken() == null) { break; } productsList.setPageToken(page.getNextPageToken()); page = productsList.execute(); }
PHP
$products = $service->products->listProducts($merchantId); $parameters = array(); while (!empty($products->getResources()) { foreach ($products->getResources() as $product) { printf("%s %s\n", $product->getId(), $product->getTitle()); } if (!empty($products->getNextPageToken()) { break; } $parameters['pageToken'] = $products->nextPageToken; $products = $service->products->listProducts($merchantId, $parameters); }
检索特定商品
要从列表中检索特定商品,请使用以下代码:
协议
GET /content/v2/YOUR_MERCHANT_ID/products/online:en:GB:book123
Java
Product product = service.products() .get(merchantId, "online:en:GB:book123") .execute(); System.out.printf("%s %s\n", product.getId(), product.getTitle());
PHP
$product = $service->products->get($merchant_id, 'online:en:GB:book123'); printf("%s %s\n", $product->getId(), $product->getTitle());
更新商品价格
您可以利用 Inventory 服务,使用以下代码更新价格和库存状况:
协议
POST /content/v2/YOUR_MERCHANT_ID/inventory/online/products/online:en:GB:book123 { "price": { "value": "3.99", "currency": "USD" } }
Java
InventorySetRequest inventory = new InventorySetRequest(); Price price = new Price(); price.setValue('3.99'); price.setCurrency('USD'); inventory.setPrice(price); service.inventory() .set(merchantId, "online", "online:en:US:book1", inventory) .execute();
PHP
$inventory = new Google_Service_ShoppingContent_InventorySetRequest(); $price = new Google_Service_ShoppingContent_Price(); $price->setValue('3.99'); $price->setCurrency('USD'); $inventory->setPrice($price); $service->inventory->set(merchant_id, 'online', 'online:en:US:book1', $inventory);