Create a product reviews data source
Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to create a product reviews data source.
Java
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shopping.merchant.samples.datasources.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.datasources.v1.CreateDataSourceRequest;
import com.google.shopping.merchant.datasources.v1.DataSource;
import com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient;
import com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings;
import com.google.shopping.merchant.datasources.v1.ProductReviewDataSource;
import java.io.IOException;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to create a product review data source. */
public class CreateProductReviewsDataSourceSample {
private static void createProductReviewsDataSource(String accountId) throws IOException {
GoogleCredentials credential = new Authenticator().authenticate();
DataSourcesServiceSettings dataSourcesServiceSettings =
DataSourcesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
try (DataSourcesServiceClient dataSourcesServiceClient =
DataSourcesServiceClient.create(dataSourcesServiceSettings)) {
CreateDataSourceRequest request =
CreateDataSourceRequest.newBuilder()
.setParent(String.format("accounts/%s", accountId))
.setDataSource(
DataSource.newBuilder()
.setDisplayName("Product Reviews Data Source")
.setProductReviewDataSource(ProductReviewDataSource.newBuilder().build())
.build())
.build();
System.out.println("Creating product reviews data source...");
DataSource dataSource = dataSourcesServiceClient.createDataSource(request);
System.out.println(
String.format("Datasource created successfully: %s", dataSource.getName()));
} catch (Exception e) {
System.out.println(e);
System.exit(1);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
createProductReviewsDataSource(config.getAccountId().toString());
}
}
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-13 UTC.
[null,null,["Last updated 2025-08-13 UTC."],[[["\u003cp\u003eThis code sample demonstrates how to create a product review data source using the Merchant API's Java client library.\u003c/p\u003e\n"],["\u003cp\u003eThe sample uses the \u003ccode\u003eDataSourcesServiceClient\u003c/code\u003e to send a \u003ccode\u003eCreateDataSourceRequest\u003c/code\u003e to create a \u003ccode\u003eDataSource\u003c/code\u003e for product reviews.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eDataSource\u003c/code\u003e is configured with a display name and set to use the \u003ccode\u003eProductReviewDataSource\u003c/code\u003e type.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication is handled through \u003ccode\u003eGoogleCredentials\u003c/code\u003e and the \u003ccode\u003eAuthenticator\u003c/code\u003e utility class, which is used to interact with the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe sample includes error handling, which will exit with an error code if creating the data source fails, and the code is readily available on GitHub for further examination.\u003c/p\u003e\n"]]],["This Java code demonstrates creating a product review data source using the Merchant API. It authenticates using Google Credentials, builds `DataSourcesServiceSettings`, and utilizes `DataSourcesServiceClient` to send a `CreateDataSourceRequest`. The request specifies the parent account ID and defines a `DataSource` with the display name \"Product Reviews Data Source\" and `ProductReviewDataSource`. Finally, it executes the `createDataSource` operation, logs the creation, and handles potential exceptions.\n"],null,["# Create a product reviews data source\n\nMerchant API code sample to create a product reviews data source. \n\n### Java\n\n // Copyright 2023 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.datasources.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.datasources.v1.CreateDataSourceRequest;\n import com.google.shopping.merchant.datasources.v1.DataSource;\n import com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient;\n import com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings;\n import com.google.shopping.merchant.datasources.v1.ProductReviewDataSource;\n import java.io.IOException;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to create a product review data source. */\n public class CreateProductReviewsDataSourceSample {\n\n private static void createProductReviewsDataSource(String accountId) throws IOException {\n\n GoogleCredentials credential = new Authenticator().authenticate();\n\n DataSourcesServiceSettings dataSourcesServiceSettings =\n DataSourcesServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n try (DataSourcesServiceClient dataSourcesServiceClient =\n DataSourcesServiceClient.create(dataSourcesServiceSettings)) {\n CreateDataSourceRequest request =\n CreateDataSourceRequest.newBuilder()\n .setParent(String.format(\"accounts/%s\", accountId))\n .setDataSource(\n DataSource.newBuilder()\n .setDisplayName(\"Product Reviews Data Source\")\n .setProductReviewDataSource(ProductReviewDataSource.newBuilder().build())\n .build())\n .build();\n\n System.out.println(\"Creating product reviews data source...\");\n DataSource dataSource = dataSourcesServiceClient.createDataSource(request);\n System.out.println(\n String.format(\"Datasource created successfully: %s\", dataSource.getName()));\n } catch (Exception e) {\n System.out.println(e);\n System.exit(1);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n createProductReviewsDataSource(config.getAccountId().toString());\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/datasources/v1/CreateProductReviewsDataSourceSample.java"]]