ความเข้ากันได้ของ Content API สำหรับ Shopping

คุณสามารถใช้คู่มือนี้เพื่อผสานรวม Merchant API กับการใช้งาน Content API for Shopping ที่มีอยู่

เริ่มต้นใช้งาน

ดูรายละเอียดเกี่ยวกับ Merchant API และ API ย่อยได้ในการออกแบบ Merchant API

หากต้องการเริ่มใช้ Merchant API ให้เปลี่ยน URL คำขอเป็นรูปแบบต่อไปนี้

https://merchantapi.googleapis.com/{SUB_API}/{VERSION}/{RESOURCE_NAME}:{METHOD}

ดูข้อมูลเพิ่มเติมได้ที่คู่มือเริ่มต้นใช้งานฉบับย่อและข้อมูลอ้างอิง Merchant API

การรองรับ gRPC

Merchant API รองรับ gRPC และ REST คุณสามารถใช้ gRPC สำหรับ Merchant API และ REST สำหรับ Content API for Shopping ได้พร้อมกัน

ไลบรารีของไคลเอ็นต์ Merchant API ต้องใช้ gRPC

ดูข้อมูลเพิ่มเติมที่ใช้ gRPC

ความเข้ากันได้

คู่มือนี้จะอธิบายการเปลี่ยนแปลงทั่วไปที่มีผลกับ Merchant API ทั้งหมด ดูคู่มือต่อไปนี้สำหรับการเปลี่ยนแปลงฟีเจอร์ที่เฉพาะเจาะจง

Merchant API ได้รับการออกแบบให้ทำงานร่วมกับฟีเจอร์ Content API for Shopping v2.1 ที่มีอยู่

ตัวอย่างเช่น คุณสามารถใช้ Merchant Inventories API ควบคู่ไปกับการใช้งาน Content API for Shopping เวอร์ชัน 2.1 ที่มีอยู่products คุณอาจใช้ Content API for Shopping เพื่ออัปโหลดผลิตภัณฑ์ใหม่ในร้าน (ที่คุณขายในร้านค้าในพื้นที่) จากนั้นใช้แหล่งข้อมูล Merchant Inventories API LocalInventoryเพื่อจัดการข้อมูลในร้านสำหรับผลิตภัณฑ์นั้น

คำขอแบบเป็นกลุ่ม

Merchant API ไม่รองรับเมธอด customBatch ที่แสดงใน Content API for Shopping โปรดดูหัวข้อส่งคําขอหลายรายการพร้อมกันหรือเรียกใช้แบบไม่พร้อมกันแทน

ตัวอย่างต่อไปนี้แสดงวิธีแทรกอินพุตผลิตภัณฑ์

Java

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.shopping.merchant.products.v1beta.Attributes;
import com.google.shopping.merchant.products.v1beta.InsertProductInputRequest;
import com.google.shopping.merchant.products.v1beta.ProductInput;
import com.google.shopping.merchant.products.v1beta.ProductInputsServiceClient;
import com.google.shopping.merchant.products.v1beta.ProductInputsServiceSettings;
import com.google.shopping.merchant.products.v1beta.Shipping;
import com.google.shopping.type.Channel.ChannelEnum;
import com.google.shopping.type.Price;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to insert a product input */
public class InsertProductInputAsyncSample {

  private static String getParent(String accountId) {
    return String.format("accounts/%s", accountId);
  }

  private static String generateRandomString() {
    String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuilder sb = new StringBuilder(8);
    for (int i = 0; i < 8; i++) {
      sb.append(characters.charAt(random.nextInt(characters.length())));
    }
    return sb.toString();
  }

  private static ProductInput createRandomProduct() {
    Price price = Price.newBuilder().setAmountMicros(33_450_000).setCurrencyCode("USD").build();

    Shipping shipping =
        Shipping.newBuilder().setPrice(price).setCountry("GB").setService("1st class post").build();

    Shipping shipping2 =
        Shipping.newBuilder().setPrice(price).setCountry("FR").setService("1st class post").build();

    Attributes attributes =
        Attributes.newBuilder()
            .setTitle("A Tale of Two Cities")
            .setDescription("A classic novel about the French Revolution")
            .setLink("https://exampleWebsite.com/tale-of-two-cities.html")
            .setImageLink("https://exampleWebsite.com/tale-of-two-cities.jpg")
            .setAvailability("in stock")
            .setCondition("new")
            .setGoogleProductCategory("Media > Books")
            .setGtin(0, "9780007350896")
            .addShipping(shipping)
            .addShipping(shipping2)
            .build();

    return ProductInput.newBuilder()
        .setChannel(ChannelEnum.ONLINE)
        .setContentLanguage("en")
        .setFeedLabel("CH")
        .setOfferId(generateRandomString())
        .setAttributes(attributes)
        .build();
  }

  public static void asyncInsertProductInput(Config config, String dataSource) throws Exception {

    // Obtains OAuth token based on the user's configuration.
    GoogleCredentials credential = new Authenticator().authenticate();

    // Creates service settings using the credentials retrieved above.
    ProductInputsServiceSettings productInputsServiceSettings =
        ProductInputsServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates parent to identify where to insert the product.
    String parent = getParent(config.getAccountId().toString());

    // Calls the API and catches and prints any network failures/errors.
    try (ProductInputsServiceClient productInputsServiceClient =
        ProductInputsServiceClient.create(productInputsServiceSettings)) {

      // Creates five insert product input requests with random product IDs.
      List<InsertProductInputRequest> requests = new ArrayList<>(5);
      for (int i = 0; i < 5; i++) {
        InsertProductInputRequest request =
            InsertProductInputRequest.newBuilder()
                .setParent(parent)
                // You can only insert products into datasource types of Input "API" and "FILE", and
                // of Type "Primary" or "Supplemental."
                // This field takes the `name` field of the datasource.
                .setDataSource(dataSource)
                // If this product is already owned by another datasource, when re-inserting, the
                // new datasource will take ownership of the product.
                .setProductInput(createRandomProduct())
                .build();

        requests.add(request);
      }

      System.out.println("Sending insert product input requests");
      List<ApiFuture<ProductInput>> futures =
          requests.stream()
              .map(
                  request ->
                      productInputsServiceClient.insertProductInputCallable().futureCall(request))
              .collect(Collectors.toList());

      // Creates callback to handle the responses when all are ready.
      ApiFuture<List<ProductInput>> responses = ApiFutures.allAsList(futures);
      ApiFutures.addCallback(
          responses,
          new ApiFutureCallback<List<ProductInput>>() {
            @Override
            public void onSuccess(List<ProductInput> results) {
              System.out.println("Inserted products below");
              System.out.println(results);
            }

            @Override
            public void onFailure(Throwable throwable) {
              System.out.println(throwable);
            }
          },
          MoreExecutors.directExecutor());

    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // Identifies the data source that will own the product input.
    String dataSource = "accounts/" + config.getAccountId() + "/dataSources/{datasourceId}";

    asyncInsertProductInput(config, dataSource);
  }
}

หากคุณใช้ customBatch ใน Content API และต้องการฟีเจอร์นี้สำหรับ Merchant API โปรดแจ้งเหตุผลให้เราทราบในความคิดเห็น

รหัสระบุ

เราได้ทําการเปลี่ยนแปลงตัวระบุแหล่งข้อมูล Merchant API บางส่วนเพื่อให้สอดคล้องกับหลักการปรับปรุง API ของ Google

ใช้ชื่อแทนรหัส

ทรัพยากร Merchant API ทั้งหมดใช้ฟิลด์ name เป็นตัวระบุที่ไม่ซ้ำกัน

ต่อไปนี้คือตัวอย่างวิธีใช้ช่อง name ในการเรียกใช้

POST https://merchantapi.googleapis.com/inventories/v1beta/{PARENT}/regionalInventories:insert

ระบบจะแสดงผลช่อง name ใหม่นี้เป็นตัวระบุทรัพยากรสําหรับการเรียกอ่านและเขียนทั้งหมดใน Merchant API

เช่น ใช้เมธอด getName() เพื่อดึงข้อมูล name จากแหล่งข้อมูล และจัดเก็บเอาต์พุตเป็นตัวแปรแทนการสร้าง name จากรหัสผู้ขายและรหัสทรัพยากรด้วยตนเอง

ช่องหลักสำหรับทรัพยากรย่อย

ใน Merchant API ทรัพยากรย่อยทั้งหมดจะมีช่อง parent คุณสามารถใช้ฟิลด์ parent เพื่อระบุ name ของทรัพยากรที่จะแทรกรายการย่อยแทนการส่งทรัพยากรหลักทั้งรายการ นอกจากนี้ คุณยังใช้ช่อง parent กับเมธอด list เพื่อแสดงรายการทรัพยากรย่อยของ parent นั้นได้ด้วย

เช่น หากต้องการแสดงสินค้าคงคลังในร้านของผลิตภัณฑ์หนึ่งๆ ให้ระบุ name ของผลิตภัณฑ์ในช่อง parent สำหรับเมธอด list ในกรณีนี้ product ที่ระบุคือ parent ของแหล่งข้อมูล LocalInventory ที่แสดงผล

ประเภท

ต่อไปนี้คือประเภททั่วไปที่แชร์ใน Merchant API ย่อย

ราคา

สิ่งที่เปลี่ยนแปลงสำหรับ Price ในแพ็กเกจ Merchant Common มีดังนี้

Content API Merchant API
ช่องจำนวนเงิน value:string amountMicros:int64
ฟิลด์สกุลเงิน currency:string currencyCode:string

ตอนนี้ระบบจะบันทึกจำนวน Price เป็นไมโคร โดยที่ 1 ล้านไมโครมีค่าเท่ากับหน่วยมาตรฐานของสกุลเงิน

ใน Content API for Shopping Price คือตัวเลขทศนิยมในรูปแบบสตริง

เปลี่ยนชื่อช่องจํานวนเงินจาก value เป็น amountMicros

เปลี่ยนชื่อช่องสกุลเงินจาก currency เป็น currencyCode แล้ว รูปแบบจะยังคงเป็น ISO 4217