תאימות של Content API for Shopping

תוכלו להשתמש במדריך הזה כדי לשלב את Merchant API עם ההטמעה הקיימת של Content API for Shopping.

Merchant API מאפשר לכם להפוך לתהליכי ניהול אוטומטיים של חשבונות, מוצרים ומלאי שטחי פרסום ב-Merchant Center. תרחישים לדוגמה לשימוש ב-Merchant API:

  • ניהול חשבון אוטומטי
  • ניהול מוצרים אוטומטי
  • ניהול אוטומטי של מלאי שטחי הפרסום
  • דוחות בהתאמה אישית

שיפורים ביחס ל-Content API

Merchant API משפר את Content API בתחומים הבאים:

שנתחיל?

במאמר העיצוב של Merchant API מפורט מידע על Merchant API ועל ממשקי ה-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 בגרסה 2.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, תוכלו לציין את הסיבה לכך במשוב.

מזהים

כדי להתאים לעקרונות השיפור של ממשקי ה-API של Google, ביצענו כמה שינויים במזהים של משאבי Merchant API.

השם מחליף את המזהה

כל המשאבים של 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 שהוחזרו.

סוגים

ריכזנו כאן כמה סוגים נפוצים ששותפו בין ממשקי ה-API המשניים של Merchant API.

מחיר

אלה השינויים ב-Price בחבילה Merchant Common:

Content API Merchant API
שדה הסכום value:string amountMicros:int64
שדה המטבע currency:string currencyCode:string

הסכום של Price מתועד עכשיו במיקרו-יחידות, כאשר מיליון מיקרו-יחידות שווה ליחידה הסטנדרטית של המטבע שלכם.

ב-Content API for Shopping, השדה Price היה מספר עשרוני בצורת מחרוזת.

שם השדה amount השתנה מ-value ל-amountMicros

שם השדה של המטבע השתנה מ-currency ל-currencyCode. הפורמט נשאר ISO 4217.