建立產品評論資料來源
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
建立產品評論資料來源的 Merchant API 程式碼範例。
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());
}
}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\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"]]