本页介绍了如何通过编程方式创建和更新数据源(包括自动数据源),以便插入商品。
借助 Content API for Shopping,您可以创建主要数据源。借助 Merchant Data Sources API,您还可以创建以下类型的数据源:
您只能使用文件输入来管理 Content API for Shopping 中的数据源。 借助 Merchant API,您可以同时使用文件和 API 输入来管理数据源。
如需详细了解与 Content API for Shopping 的对比,请参阅迁移数据源管理。
使用 Merchant Data sources API,您可以执行以下操作:
- 使用特定的
feedLabel
和contentLanguage
创建主要数据源。 - 创建一个未设置
feedLabel
和contentLanguage
字段的数据源。使用此类数据源,您可以为商品指定多个目标国家/地区,因为您可以将具有不同feedLabel
和contentLanguage
组合的商品插入到单个数据源中。 - 创建补充数据源以关联到现有的主要数据源。
- 为文件数据源设置时间表。
- 注册您的账号,以便自动管理数据源。
- 管理 API 数据源。
- 使用主要商品数据源管理数据源的默认规则。
- 为商品数据源自定义
destinations
(也称为“营销方式”)。 - 使用其他类型的数据源,例如促销活动。
- 使用特定的
feedLabel
和contentLanguage
创建主要数据源。 - 创建一个未设置
feedLabel
和contentLanguage
字段的数据源。使用此类数据源,您可以为商品指定多个目标国家/地区,因为您可以将具有不同feedLabel
和contentLanguage
组合的商品插入到单个数据源中。 - 创建补充数据源以关联到现有的主要数据源。
- 为文件数据源设置时间表。
- 注册您的账号,以便自动管理数据源。
- 管理 API 数据源。
- 使用主要商品数据源管理数据源的默认规则。
- 为商品数据源自定义
destinations
(也称为“营销方式”)。 - 使用其他类型的数据源,例如促销活动。
主要数据源是 Merchant Center 商品目录的主要数据源。您只能使用主要数据源添加或移除商品。如果您添加到主要数据源中的每件商品都符合 Merchant Center 的数据和资格要求,则无需再创建更多数据源。
如需创建具有特定 feedLabel
和 contentLanguage
的主要数据源,请在类型专用配置中设置 feedLabel
和 contentLanguage
字段。如需详细了解这些字段,请参阅 PrimaryProductDataSource
。
以下示例请求演示了如何创建主要商品数据源:
如需详细了解这些字段,请参阅 PrimaryProductDataSource
。
以下示例请求演示了如何创建主要商品数据源:
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"
}
}
替换以下内容:
- {ACCOUNT_ID}:您的 Merchant Center 账号的唯一标识符。
- {DISPLAY_NAME}:数据源的显示名称。
- {CONTENT_LANGUAGE}:数据源中商品的双字母 ISO 639-1 语言代码。
- {FEED_LABEL}:数据源的 Feed 标签。
- {COUNTRY}:将使用数据源上传的商品的目标国家/地区的 CLDR 地区代码。
请求成功运行后,您会看到以下响应:
{
"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}"
],
"destinations": [
{
"id": "SHOPPING_ADS",
"state": "ENABLED"
},
{
"id": "FREE_LISTINGS",
"state": "ENABLED"
}
]
"defaultRule": {
"takeFromDataSources": [
{
"self": true
}
]
}
},
"input": "API"
}
destinations
字段不为空,因为为数据源启用了默认目标位置。
如需详细了解如何创建数据源,请参阅 accounts.dataSources.create 方法。
如需查看新创建的数据源,请使用 accounts.dataSources.get 或 accounts.dataSources.list 方法。
以下示例演示了如何为 GB
和 en
feedLabel
和 contentLanguage
组合创建主要商品数据源。
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);
}
}
创建主要数据源,以便定位到多个国家/地区
若要创建可帮助您定位到多个国家/地区的主要 Feed,请使用 PrimaryProductDataSource
配置数据源,并勿设置 feedLabel
和 contentLanguage
字段。
使用 Content API for Shopping 时,系统只会为您创建一个 API 数据源。使用 Merchant Data Sources API,您可以拥有多个 API 数据源,其中一些 API 数据源可以不设置 feedLabel
和 contentLanguage
字段。
只有使用 API 输入的数据源可以不设置 feedLabel
和 contentLanguage
字段。文件输入不支持此类数据源。
创建补充数据源并将其关联到主要数据源
补充数据源仅用于更新一个或多个主要数据源中已存在的商品数据。您可以提供多个补充数据源,且每个数据源可以补充任意个主要数据源中的数据。
您可以使用补充数据源对商品数据进行部分更新,方法是在调用 accounts.productInputs.insert
和 accounts.productInputs.delete
方法时将数据源的唯一标识符添加为查询参数。您只能使用补充数据源更新现有商品。
如需创建补充数据源,请使用 SupplementalProductDataSource
配置数据源,然后通过更新主要数据源的 defaultRule
字段将其关联起来。
补充文件数据源必须设置 feedLabel
和 contentLanguage
字段。补充 API 数据源的 feedLabel
和 contentLanguage
字段始终必须处于未设置状态。
以下示例演示了如何为 en
和 GB
contentLanguage
和 feedLabel
组合创建文件补充商品数据源。
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);
}
}
如需创建适用于所有 feedLabel
和 contentLanguage
组合的补充数据源,请运行以下示例。
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);
}
}
为文件数据源设置时间表
如需为文件 Feed 设置时间表,请使用 FileInput
字段将数据源配置为文件数据源,然后使用 FileInput.FetchSettings
字段设置 fetchsettings
。
删除数据源
如需从您的账号中删除现有数据源,请使用 accounts.dataSources.delete
方法。
以下示例演示了如何使用 DeleteDataSourceRequest
软件包删除数据源。
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);
}
}
提取数据源
如需提取在数据源中配置的文件,请使用 accounts.dataSources.fetch
方法。此方法会立即对您账号中的数据源执行数据提取。此方法仅适用于设置了文件输入的数据源。
获取数据源
如需检索账号的数据源配置,请使用 accounts.dataSources.get
方法。
以下示例演示了如何使用 GetDataSourceRequest
软件包为给定的 Merchant Center 账号检索特定数据源。
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);
}
}
列出数据源
如需列出您账号的数据源配置,请使用 accounts.dataSources.list
方法。
以下示例演示了如何使用 ListDataSourceRequest
软件包列出给定 Merchant Center 账号的所有数据源。
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);
}
}
补丁数据源
如需更新现有数据源的配置,请使用 accounts.dataSources.patch
方法。
如需更新现有数据源的显示名称,请运行以下 PATCH
请求。
PATCH https://merchantapi.googleapis.com/datasources/v1beta/accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}?updateMask=displayName
{
"displayName": "New display name"
}
系统会返回以下 JSON 响应。该请求仅更新数据源的 displayName
,所有其他字段均保持不变。
{
"name": "accounts/ACCOUNT_ID/dataSources/DATASOURCE_ID",
"dataSourceId": "DATASOURCE_ID",
"displayName": "New display name",
"primaryProductDataSource": {
"channel": "ONLINE_PRODUCTS",
"feedLabel": "IN",
"contentLanguage": "en",
"defaultRule": {
"takeFromDataSources": [
{
"self": true
}
]
},
"destinations": [
{
"destination": "FREE_LISTINGS",
"state": "ENABLED"
}
]
},
"input": "API"
}
以下示例演示了如何使用 UpdateDataSourceRequest
软件包更新数据源。该视频还演示了如何更新主要数据源,以将补充数据源添加到其默认规则。
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);
}
}
管理数据源规则
借助主要商品数据源,您可以管理数据源的默认规则。默认规则是指应用于数据源中所有属性的规则。您可以在创建数据源时设置默认规则,也可以通过“默认规则”字段更新现有数据源。
如需详细了解如何设置规则,请参阅为您的商品数据源设置规则。
以下示例配置可确保先从具有唯一标识符 1001
的数据源中获取所有属性。然后,系统会从主要数据源添加缺失的属性。最后,如果任何其他数据源中尚未提供剩余属性,系统会从具有唯一标识符 1002
的补充数据源中获取这些属性。如果多个数据源中提供了相同的属性,系统会选择列表中较高的值。
defaultRule {
takeFromDataSources: [
'1001', // Supplemental product data source
'self', // Self reference to the primary data source
'1002' // Supplemental product data source
]
}
自动管理数据源
借助自动数据源,您可以更轻松地向 Google 发送商品数据。这些数据源可确保 Google 获取您网站上有关相关商品的最新信息。
如需为您的账号注册自动管理数据源,您必须执行以下操作:
- 调用
accounts.autofeedSettings.getAutofeedSettings
方法,检查您的账号是否符合注册条件。 - 请确保您的账号不是购物平台账号。
您的账号符合注册条件后,请执行以下操作:
启用自动 Feed 设置以自动创建数据源。注意:在更新请求中,
Name
字段应为空。在update_mask field
中,列出要更改的字段(例如enableProducts
)。然后,在正文中提供实际值(例如enableProducts=false
)。PATCH https://merchantapi.googleapis.com/accounts/v1beta/accounts/{ACCOUNT_ID}/autofeedSettings?updateMask={UPDATE_MASK} { "enableProducts": {ENABLE_PRODUCTS} }
替换以下内容:
- {ACCOUNT_ID}:您的 Merchant Center 账号的唯一标识符。
- {UPDATE_MASK}:要更改的字段列表。
- {ENABLE_PRODUCTS}:是否启用商品自动管理功能。
使用
accounts.autofeedSettings.updateAutofeedSettings
方法可实现对数据源的自动管理。启用自动管理数据源功能后,Google 会自动添加您网店中的商品,并确保这些商品在 Google 平台上始终保持最新状态。
检索文件上传状态
如需获取使用文件、提取或电子表格的数据源的状态,您可以调用 accounts.dataSources.fileUploads
服务的 GET
方法。如需在数据源处理完成时异步计算数据源上次检索的结果,请使用名称标识符 latest
。
GET https://merchantapi.googleapis.com/accounts/v1beta/{ACCOUNT_ID}/datasources/{DATASOURCE_ID}/fileUploads/latest
文件上传状态可能包含商品的详细视图,包括任何潜在问题。
请注意,如果文件从未上传,则可能不存在文件上传状态。 如果您在文件上传后立即请求,文件上传状态可能会处于“处理中”状态。