Tổng quan về API Nguồn dữ liệu của người bán

Nguồn dữ liệu tự động giúp bạn gửi dữ liệu sản phẩm đến Google dễ dàng hơn. Những nguồn dữ liệu này đảm bảo rằng thông tin mới nhất về các sản phẩm có liên quan trên trang web của bạn sẽ được gửi đến Google.

Trang này cho biết cách tạo và cập nhật nguồn dữ liệu theo phương thức lập trình để bạn có thể chèn sản phẩm.

Content API for Shopping cho phép bạn tạo nguồn dữ liệu chính. Với Merchant Data Sources API, bạn cũng có thể tạo các loại nguồn dữ liệu sau:

Content API for Shopping chỉ cho phép bạn quản lý các nguồn dữ liệu có phương thức nhập tệp. Merchant API cho phép bạn quản lý nguồn dữ liệu bằng cả dữ liệu đầu vào dạng tệp và API.

Để biết thêm thông tin về sự khác biệt giữa tính năng này với Content API for Shopping, hãy xem bài viết Di chuyển tính năng quản lý nguồn dữ liệu.

Khi sử dụng API Nguồn dữ liệu người bán, bạn có thể làm những việc sau:

  • Tạo một nguồn dữ liệu chính bằng một feedLabelcontentLanguage cụ thể.
  • Tạo một nguồn dữ liệu không đặt các trường feedLabelcontentLanguage. Khi sử dụng loại nguồn dữ liệu này, bạn có thể nhắm đến nhiều quốc gia cho sản phẩm của mình, vì bạn có thể chèn các sản phẩm có nhiều tổ hợp feedLabelcontentLanguage vào một nguồn dữ liệu.
  • Tạo một nguồn dữ liệu bổ sung để liên kết với một nguồn dữ liệu chính hiện có.
  • Thiết lập lịch biểu cho nguồn dữ liệu tệp.
  • Đăng ký tài khoản của bạn để tự động quản lý các nguồn dữ liệu.
  • Quản lý nguồn dữ liệu API.
  • Quản lý quy tắc mặc định của nguồn dữ liệu bằng nguồn dữ liệu sản phẩm chính.
  • Sử dụng các loại nguồn dữ liệu khác như chương trình khuyến mãi.

Để biết thêm thông tin về kênh nguồn dữ liệu, hãy xem phần Kênh.

Điều kiện tiên quyết

  • Tài khoản của bạn phải được di chuyển sang nguồn cấp dữ liệu theo ngôn ngữ.
  • Để xác minh rằng tài khoản đã được di chuyển sang tính năng phân tách mục tiêu dữ liệu, hãy sử dụng danh sách nguồn dữ liệu hoặc các phương thức lấy. Trong trường hợp không đủ điều kiện, bạn sẽ nhận được thông báo ngoại lệ sau và nên liên hệ với nhóm hỗ trợ.

    This account is in the data sources migration process and can't be used with
    this API yet. Contact support for more info on when this account will be able
    to use the data sources endpoint.
    

Tạo một nguồn dữ liệu

Nguồn dữ liệu chính là nguồn dữ liệu chính cho kho hàng của bạn trên Merchant Center. Bạn chỉ có thể thêm hoặc xoá sản phẩm bằng nguồn dữ liệu chính. Nếu mỗi sản phẩm bạn thêm vào nguồn dữ liệu chính đều đáp ứng các yêu cầu về dữ liệu và tiêu chí của Merchant Center, bạn sẽ không cần phải tạo thêm bất kỳ nguồn dữ liệu nào.

Để tạo một nguồn dữ liệu chính có feedLabelcontentLanguage cụ thể, hãy đặt các trường feedLabelcontentLanguage trong cấu hình dành riêng cho loại. Để biết thêm thông tin về các trường này, hãy xem PrimaryProductDataSource.

Yêu cầu mẫu sau đây minh hoạ cách tạo nguồn dữ liệu sản phẩm chính:

POST https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources

{
  "displayName": "{DISPLAY_NAME}",
  "primaryProductDataSource": {
    "contentLanguage": "{CONTENT_LANGUAGE}",
    "feedLabel": "{FEED_LABEL}",
    "countries": [
      "{COUNTRY}"
    ],
    "channel": "ONLINE_PRODUCTS"
  }
}

Thay thế nội dung sau:

  • {ACCOUNT_ID}: Giá trị nhận dạng duy nhất của tài khoản Merchant Center.
  • {DISPLAY_NAME}: Tên hiển thị của nguồn dữ liệu.
  • {CONTENT_LANGUAGE}: Mã ngôn ngữ ISO 639-1 gồm hai chữ cái của các sản phẩm trong nguồn dữ liệu.
  • {FEED_LABEL}: Nhãn nguồn cấp dữ liệu của nguồn dữ liệu.
  • {COUNTRY}: Mã lãnh thổ CLDR của quốc gia mục tiêu của các sản phẩm sẽ được tải lên bằng nguồn dữ liệu.

Sau khi yêu cầu chạy thành công, bạn sẽ thấy phản hồi sau:

{
  "name": "accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}",
  "dataSourceId": "{DATASOURCE_ID}",
  "displayName": "{DISPLAY_NAME}",
  "primaryProductDataSource": {
    "channel": "ONLINE_PRODUCTS",
    "feedLabel": "{FEED_LABEL}",
    "contentLanguage": "{CONTENT_LANGUAGE}",
    "countries": [
      "{COUNTRY}"
    ],
    "defaultRule": {
      "takeFromDataSources": [
        {
          "self": true
        }
      ]
    }
  },
  "input": "API"
}

Để biết thêm thông tin về cách tạo nguồn dữ liệu, hãy xem phương thức accounts.dataSources.create.

Để xem nguồn dữ liệu mới tạo, hãy sử dụng phương thức accounts.dataSources.get hoặc accounts.dataSources.list.

Mẫu sau đây minh hoạ cách bạn có thể tạo nguồn dữ liệu sản phẩm chính cho tổ hợp GBen feedLabelcontentLanguage.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to create a primary product datasource for all `feedLabel` and
 * `contentLanguage` combinations. Note that rules functionality is limited for wildcard feeds.
 */
public class CreatePrimaryProductDataSourceWildCardSample {

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

  public static String createDataSource(Config config, String displayName) throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getAccountId().toString());

    // The type of data that this datasource will receive.
    PrimaryProductDataSource primaryProductDataSource =
        PrimaryProductDataSource.newBuilder()
            // Channel can be "ONLINE_PRODUCTS" or "LOCAL_PRODUCTS" or "PRODUCTS" .
            // While accepted, datasources with channel "products" representing unified products
            // currently cannot be used with the Products bundle.
            .setChannel(PrimaryProductDataSource.Channel.ONLINE_PRODUCTS)
            .addCountries("GB")
            .build();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      CreateDataSourceRequest request =
          CreateDataSourceRequest.newBuilder()
              .setParent(parent)
              .setDataSource(
                  DataSource.newBuilder()
                      .setDisplayName(displayName)
                      .setPrimaryProductDataSource(primaryProductDataSource)
                      .build())
              .build();

      System.out.println("Sending Create PrimaryProduct DataSource request");
      DataSource response = dataSourcesServiceClient.createDataSource(request);
      System.out.println("Created DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      // Null is necessary to satisfy the compiler as we're not returning a String on failure.
      return null;
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The displayed datasource name in the Merchant Center UI.
    String displayName = "Primary Product Data Wildcard";

    createDataSource(config, displayName);
  }
}

Tạo một nguồn dữ liệu chính mới giúp nhắm đến nhiều quốc gia

Để tạo một nguồn cấp dữ liệu chính mới giúp bạn nhắm đến nhiều quốc gia, hãy định cấu hình nguồn dữ liệu bằng PrimaryProductDataSource và không đặt các trường feedLabelcontentLanguage.

Khi sử dụng Content API for Shopping, hệ thống sẽ chỉ tạo một nguồn dữ liệu API cho bạn. Khi sử dụng API Nguồn dữ liệu người bán, bạn có thể có nhiều nguồn dữ liệu API, trong đó một số nguồn dữ liệu có thể không đặt trường feedLabelcontentLanguage.

Chỉ những nguồn dữ liệu có dữ liệu đầu vào API mới có thể không đặt trường feedLabelcontentLanguage. Loại nguồn dữ liệu này không được hỗ trợ cho dữ liệu đầu vào tệp.

Tạo nguồn dữ liệu bổ sung và liên kết nguồn dữ liệu đó với nguồn dữ liệu chính

Nguồn dữ liệu bổ sung chỉ dùng để cập nhật dữ liệu sản phẩm đã có trong một hoặc nhiều nguồn dữ liệu chính. Bạn có thể có nhiều nguồn dữ liệu bổ sung và mỗi nguồn dữ liệu bổ sung có thể bổ sung dữ liệu cho số lượng nguồn dữ liệu chính bất kỳ.

Bạn có thể sử dụng nguồn dữ liệu bổ sung để cập nhật một phần dữ liệu sản phẩm bằng cách thêm giá trị nhận dạng duy nhất của nguồn dữ liệu làm tham số truy vấn khi thực hiện lệnh gọi đến các phương thức accounts.productInputs.insertaccounts.productInputs.delete. Bạn chỉ có thể sử dụng nguồn dữ liệu bổ sung để cập nhật các sản phẩm hiện có.

Để tạo nguồn dữ liệu bổ sung, hãy định cấu hình nguồn dữ liệu bằng cách sử dụng SupplementalProductDataSource, sau đó liên kết nguồn dữ liệu đó bằng cách cập nhật trường defaultRule trên nguồn dữ liệu chính.

Nguồn dữ liệu tệp bổ sung phải đặt các trường feedLabelcontentLanguage. Nguồn dữ liệu API bổ sung phải luôn đặt trường feedLabelcontentLanguage thành không xác định.

Mẫu sau đây minh hoạ cách bạn có thể tạo nguồn dữ liệu sản phẩm bổ sung của tệp cho tổ hợp enGB contentLanguagefeedLabel.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.FileInput;
import com.google.shopping.merchant.datasources.v1beta.SupplementalProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to create a File Supplemental product datasource for the "en" and
 * "GB" `feedLabel` and `contentLanguage` combination. This supplemental feed is eligible to be
 * linked to both a wildcard primary feed and/or a primary feed with the same `feedLabel` and
 * `contentLanguage` combination.
 */
public class CreateFileSupplementalProductDataSourceSample {

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

  private static FileInput setFileInput() {
    // If FetchSettings are not set, then this will be an `UPLOAD` file type
    // that you must manually upload via the Merchant Center UI.
    return FileInput.newBuilder()
        // FileName is required for `UPLOAD` fileInput type.
        .setFileName("British T-shirts Supplemental Data")
        .build();
  }

  public static String createDataSource(Config config, String displayName, FileInput fileInput)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getAccountId().toString());

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      CreateDataSourceRequest request =
          CreateDataSourceRequest.newBuilder()
              .setParent(parent)
              .setDataSource(
                  DataSource.newBuilder()
                      .setDisplayName(displayName)
                      .setSupplementalProductDataSource(
                          SupplementalProductDataSource.newBuilder()
                              .setContentLanguage("en")
                              .setFeedLabel("GB")
                              .build())
                      .setFileInput(fileInput)
                      .build())
              .build();

      System.out.println("Sending create SupplementalProduct DataSource request");
      DataSource response = dataSourcesServiceClient.createDataSource(request);
      System.out.println("Created DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      // Null is necessary to satisfy the compiler as we're not returning a String on failure.
      return null;
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The displayed datasource name in the Merchant Center UI.
    String displayName = "British File Supplemental Product Data";

    // The file input data that this datasource will receive.
    FileInput fileInput = setFileInput();

    createDataSource(config, displayName, fileInput);
  }
}

Để tạo một nguồn dữ liệu bổ sung hoạt động cho tất cả các tổ hợp feedLabelcontentLanguage, hãy chạy mẫu sau.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.SupplementalProductDataSource;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to create a Supplemental product datasource all `feedLabel` and
 * `contentLanguage` combinations. This works only for API supplemental feeds.
 */
public class CreateSupplementalProductDataSourceWildCardSample {

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

  public static String createDataSource(Config config, String displayName) throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String parent = getParent(config.getAccountId().toString());

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      CreateDataSourceRequest request =
          CreateDataSourceRequest.newBuilder()
              .setParent(parent)
              .setDataSource(
                  DataSource.newBuilder()
                      .setDisplayName(displayName)
                      .setSupplementalProductDataSource(
                          SupplementalProductDataSource.newBuilder().build())
                      .build())
              .build();

      System.out.println("Sending create SupplementalProduct DataSource request");
      DataSource response = dataSourcesServiceClient.createDataSource(request);
      System.out.println("Created DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null; // Necessary to satisfy the compiler as we're not returning a
      // String on failure.
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The displayed datasource name in the Merchant Center UI.
    String displayName = "Supplemental API Product Data Wildcard";

    createDataSource(config, displayName);
  }
}

Thiết lập lịch biểu cho nguồn dữ liệu tệp

Để thiết lập lịch biểu cho nguồn cấp dữ liệu tệp, hãy định cấu hình nguồn dữ liệu của bạn thành nguồn dữ liệu tệp bằng cách sử dụng trường FileInput, sau đó thiết lập fetchsettings bằng cách sử dụng trường FileInput.FetchSettings.

Xoá nguồn dữ liệu

Để xoá một nguồn dữ liệu hiện có khỏi tài khoản, hãy sử dụng phương thức accounts.dataSources.delete.

Mẫu sau đây minh hoạ cách bạn có thể sử dụng gói DeleteDataSourceRequest để xoá nguồn dữ liệu.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.DeleteDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to delete a datasource. */
public class DeleteDataSourceSample {

  public static void deleteDataSource(Config config, String dataSourceId) throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
      DeleteDataSourceRequest request = DeleteDataSourceRequest.newBuilder().setName(name).build();

      System.out.println("Sending deleteDataSource request");
      // Delete works for any datasource type.
      // If Type "Supplemental", delete will only work if it's not linked to any primary feed.
      // If a link exists and the Type is "Supplemental", you will need to remove the supplemental
      // feed from the default and/or custom rule(s) of any primary feed(s) that references it. Then
      // retry the delete.

      dataSourcesServiceClient.deleteDataSource(request); // No response returned on success.
      System.out.println(
          "Delete successful, note that it may take a few minutes for the delete to update in"
              + " the system.");
    } catch (Exception e) {
      System.out.println(e);
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // An ID automatically assigned to the datasource after creation by Google.
    String dataSourceId = "1111111111"; // Replace with your datasource ID.

    deleteDataSource(config, dataSourceId);
  }
}

Tìm nạp nguồn dữ liệu

Để tìm nạp một tệp được định cấu hình trong nguồn dữ liệu, hãy sử dụng phương thức accounts.dataSources.fetch. Phương thức này thực hiện tìm nạp dữ liệu ngay lập tức trên một nguồn dữ liệu từ tài khoản của bạn. Phương thức này chỉ hoạt động trên các nguồn dữ liệu có tập hợp đầu vào tệp.

Lấy nguồn dữ liệu

Để truy xuất cấu hình nguồn dữ liệu cho tài khoản của bạn, hãy sử dụng phương thức accounts.dataSources.get.

Mẫu sau đây minh hoạ cách bạn có thể sử dụng gói GetDataSourceRequest để truy xuất một nguồn dữ liệu cụ thể cho một tài khoản Merchant Center nhất định.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.GetDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to get a specific datasource for a given Merchant Center account. */
public class GetDataSourceSample {

  public static DataSource getDataSource(Config config, String dataSourceId) throws Exception {

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

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

    // Creates datasource name to identify datasource.
    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    // Calls the API and catches and prints any network failures/errors.
    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      // The name has the format: accounts/{account}/datasources/{datasource}
      GetDataSourceRequest request = GetDataSourceRequest.newBuilder().setName(name).build();

      System.out.println("Sending GET DataSource request:");
      DataSource response = dataSourcesServiceClient.getDataSource(request);

      System.out.println("Retrieved DataSource below");
      System.out.println(response);
      return response;
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null; // Necessary to satisfy the compiler as we're not returning a
      // DataSource on failure.
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // An ID assigned to a datasource by Google.
    String datasourceId = "1111111111"; // Replace with your datasource ID.

    getDataSource(config, datasourceId);
  }
}

Danh sách nguồn dữ liệu

Để liệt kê các cấu hình cho nguồn dữ liệu của tài khoản, hãy sử dụng phương thức accounts.dataSources.list.

Mẫu sau đây minh hoạ cách bạn có thể sử dụng gói ListDataSourceRequest để liệt kê tất cả các nguồn dữ liệu cho một tài khoản Merchant Center nhất định.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient.ListDataSourcesPagedResponse;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.ListDataSourcesRequest;
import java.util.ArrayList;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/** This class demonstrates how to list all the datasources for a given Merchant Center account */
public class ListDataSourcesSample {

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

  public static ArrayList<DataSource> listDataSources(Config config) throws Exception {

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

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

    // Creates parent to identify the account from which to list all the datasources.
    String parent = getParent(config.getAccountId().toString());

    // Calls the API and catches and prints any network failures/errors.
    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      // The parent has the format: accounts/{account}
      ListDataSourcesRequest request =
          ListDataSourcesRequest.newBuilder().setParent(parent).build();

      System.out.println("Sending list datasources request:");
      ListDataSourcesPagedResponse response = dataSourcesServiceClient.listDataSources(request);

      int count = 0;
      ArrayList<DataSource> dataSources = new ArrayList<DataSource>();
      ArrayList<DataSource> justPrimaryDataSources = new ArrayList<DataSource>();

      // Iterates over all rows in all pages and prints the datasource in each row.
      // Automatically uses the `nextPageToken` if returned to fetch all pages of data.
      for (DataSource element : response.iterateAll()) {
        System.out.println(element);
        count++;
        dataSources.add(element);
        // The below lines show how to filter datasources based on type.
        // `element.hasSupplementalProductDataSource()` would give you supplemental
        // datasources, etc.
        if (element.hasPrimaryProductDataSource()) {
          justPrimaryDataSources.add(element);
        }
      }
      System.out.print("The following count of elements were returned: ");
      System.out.println(count);
      return dataSources;
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null; // Necessary to satisfy the compiler as we're not returning an
      // ArrayList<DataSource> on failure.
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();

    listDataSources(config);
  }
}

Nguồn dữ liệu bản vá

Để cập nhật cấu hình của một nguồn dữ liệu hiện có, hãy sử dụng phương thức accounts.dataSources.patch.

Mẫu sau đây minh hoạ cách bạn có thể sử dụng gói UpdateDataSourceRequest để cập nhật nguồn dữ liệu. Bài viết này cũng minh hoạ cách cập nhật nguồn dữ liệu chính để thêm nguồn dữ liệu bổ sung vào quy tắc mặc định.

Java

   import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.datasources.v1beta.DataSource;
import com.google.shopping.merchant.datasources.v1beta.DataSourceName;
import com.google.shopping.merchant.datasources.v1beta.DataSourceReference;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1beta.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource;
import com.google.shopping.merchant.datasources.v1beta.PrimaryProductDataSource.DefaultRule;
import com.google.shopping.merchant.datasources.v1beta.UpdateDataSourceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;

/**
 * This class demonstrates how to update a datasource to change its name in the MC UI. It also
 * demonstrates how to update a primary datasource to add supplemental datasources to its default
 * rule (https://support.google.com/merchants/answer/7450276).
 */
public class UpdateDataSourceSample {

  public static String updateDataSource(Config config, String displayName, String dataSourceId)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Creates datasource name to identify datasource.
    String name =
        DataSourceName.newBuilder()
            .setAccount(config.getAccountId().toString())
            .setDatasource(dataSourceId)
            .build()
            .toString();

    DataSource dataSource =
        DataSource.newBuilder()
            // Update the datasource to have the new display name
            .setDisplayName(displayName)
            .setName(name)
            .build();

    FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      UpdateDataSourceRequest request =
          UpdateDataSourceRequest.newBuilder()
              .setDataSource(dataSource)
              .setUpdateMask(fieldMask)
              .build();

      System.out.println("Sending Update DataSource request");
      DataSource response = dataSourcesServiceClient.updateDataSource(request);
      System.out.println("Updated DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null;
    }
  }

  public String updateDataSource(
      Config config,
      String primaryDataSourceName,
      String firstSupplementalDataSourceName,
      String secondSupplementalDataSourceName)
      throws Exception {
    GoogleCredentials credential = new Authenticator().authenticate();

    DataSourcesServiceSettings dataSourcesServiceSettings =
        DataSourcesServiceSettings.newBuilder()
            .setCredentialsProvider(FixedCredentialsProvider.create(credential))
            .build();

    // Setting self to 'true' refers to the primary datasource itself.
    DataSourceReference dataSourceReferenceSelf =
        DataSourceReference.newBuilder().setSelf(true).build();
    DataSourceReference firstSupplementalDataSourceReference =
        DataSourceReference.newBuilder()
            .setSupplementalDataSourceName(firstSupplementalDataSourceName)
            .build();
    DataSourceReference secondSupplementalDataSourceReference =
        DataSourceReference.newBuilder()
            .setSupplementalDataSourceName(secondSupplementalDataSourceName)
            .build();

    // The attributes will first be taken from the primary DataSource.
    // Then the first supplemental DataSource if the attribute is not in the primary DataSource
    // And finally the second supplemental DataSource if not in the first two DataSources.
    // Note that CustomRules could change the behavior of how updates are applied.
    DefaultRule defaultRule =
        DefaultRule.newBuilder()
            .addTakeFromDataSources(dataSourceReferenceSelf)
            .addTakeFromDataSources(firstSupplementalDataSourceReference)
            .addTakeFromDataSources(secondSupplementalDataSourceReference)
            .build();

    // The type of data that this datasource will receive.
    PrimaryProductDataSource primaryProductDataSource =
        PrimaryProductDataSource.newBuilder().setDefaultRule(defaultRule).build();

    DataSource dataSource =
        DataSource.newBuilder()
            // Update the primary datasource to have the default rule datasources in the correct
            // order.
            .setPrimaryProductDataSource(primaryProductDataSource)
            .setName(primaryDataSourceName)
            .build();

    // The '.' signifies a nested field.
    FieldMask fieldMask =
        FieldMask.newBuilder().addPaths("primary_product_data_source.default_rule").build();

    try (DataSourcesServiceClient dataSourcesServiceClient =
        DataSourcesServiceClient.create(dataSourcesServiceSettings)) {

      UpdateDataSourceRequest request =
          UpdateDataSourceRequest.newBuilder()
              .setDataSource(dataSource)
              .setUpdateMask(fieldMask)
              .build();

      System.out.println("Sending Update DataSource request");
      DataSource response = dataSourcesServiceClient.updateDataSource(request);
      System.out.println("Updated DataSource Name below");
      System.out.println(response.getName());
      return response.getName();
    } catch (Exception e) {
      System.out.println(e);
      System.exit(1);
      return null;
    }
  }

  public static void main(String[] args) throws Exception {
    Config config = Config.load();
    // The updated displayed datasource name in the Merchant Center UI.
    String displayName = "Great Britain Primary Product Data";

    // The ID of the datasource to update
    String dataSourceId = "11111111"; // Replace with your datasource ID.

    updateDataSource(config, displayName, dataSourceId);
  }
}

Nguồn dữ liệu sản phẩm chính cho phép bạn quản lý quy tắc mặc định của nguồn dữ liệu. Quy tắc mặc định là quy tắc áp dụng cho tất cả thuộc tính trong nguồn dữ liệu của bạn. Bạn có thể đặt quy tắc mặc định trong khi tạo nguồn dữ liệu hoặc bằng cách cập nhật nguồn dữ liệu hiện có thông qua trường quy tắc mặc định.

Để biết thêm thông tin về cách thiết lập quy tắc, hãy xem bài viết Thiết lập quy tắc cho nguồn dữ liệu sản phẩm.

Cấu hình mẫu sau đây đảm bảo rằng tất cả các thuộc tính được lấy từ nguồn dữ liệu có giá trị nhận dạng duy nhất 1001. Sau đó, các thuộc tính còn thiếu sẽ được thêm từ nguồn dữ liệu chính. Cuối cùng, các thuộc tính còn lại sẽ được lấy từ nguồn dữ liệu bổ sung có giá trị nhận dạng duy nhất 1002 nếu chưa được cung cấp trong bất kỳ nguồn dữ liệu nào khác. Nếu cùng một thuộc tính được cung cấp trong nhiều nguồn dữ liệu, thì giá trị cao hơn trong danh sách sẽ được chọn.

defaultRule {
 takeFromDataSources: [
   '1001', // Supplemental product data source
   'self', //  Self reference to the primary data source
   '1002' // Supplemental product data source
 ]
}

Tự động quản lý nguồn cấp dữ liệu

Để đăng ký tài khoản của mình để tự động quản lý các nguồn dữ liệu, bạn phải làm như sau:

Sau khi tài khoản của bạn đủ điều kiện đăng ký, bạn có thể sử dụng phương thức accounts.autofeedSettings.updateAutofeedSettings để bật tính năng tự động quản lý nguồn dữ liệu. Khi bạn bật tính năng tự động quản lý nguồn dữ liệu, Google sẽ tự động thêm sản phẩm từ cửa hàng trực tuyến của bạn và đảm bảo sản phẩm luôn được cập nhật trên các nền tảng của Google.

Truy xuất trạng thái tải tệp lên

Để biết trạng thái của nguồn dữ liệu bằng tệp, tìm nạp hoặc bảng tính, bạn có thể gọi phương thức GET của dịch vụ accounts.dataSources.fileUploads. Để nhận kết quả của lần truy xuất gần đây nhất của nguồn dữ liệu được tính toán không đồng bộ khi quá trình xử lý nguồn dữ liệu kết thúc, hãy sử dụng giá trị nhận dạng tên latest.

GET https://merchantapi.googleapis.com/accounts/v1beta/{ACCOUNT_ID}/datasources/{DATASOURCE_ID}/fileUploads/latest

Trạng thái tải tệp lên có thể chứa thông tin chi tiết về sản phẩm của bạn, bao gồm cả mọi vấn đề tiềm ẩn.

Xin lưu ý rằng trạng thái tải tệp lên có thể không tồn tại nếu tệp chưa bao giờ được tải lên. Trạng thái tải tệp lên có thể ở trạng thái đang xử lý nếu bạn yêu cầu ngay sau khi tải tệp lên.