商品评价对于客户而言是购物体验的重要组成部分 。这些评分和评价有助于客户研究商品和做出购买决定。正面商品评价可以吸引更多优质客户访问卖家的商品页面。评价来源包括卖家、评价聚合平台、评价网站和 Google 用户。
本页面介绍了如何使用 Merchant API 管理商品评价。
前提条件
Google 需要您提供特定信息。您必须具备以下条件:
- 在 Google Merchant Center 中拥有有效的商品评价 Feed。
- 您的账号必须已加入商品评分计划。您可以使用 Programs 子 API 以编程方式检查资格,也可以通过 Merchant Center 进行检查。如果您不符合资格,请详细了解 如何加入商品评分计划。
- 如需使用 Merchant API 查看商品,请在 Shopping API 支持表单 的“问题/疑问是什么?”下提交许可名单请求。
创建数据源
使用 datasource.create 方法创建商品评价 Feed。如果有现有的商家或商品评价 Feed,请使用
accounts.dataSources.list
获取 accounts.dataSources.name。将您创建或检索到的数据源名称存储在本地数据库中,以供每条评价使用。请求的格式如下:
POST https://merchantapi.googleapis.com/datasources/v1/accounts/{ACCOUNT_ID}/dataSources
示例
该示例展示了一个典型的请求和响应:
请求
POST https://merchantapi.googleapis.com/datasources/v1/accounts/{ACCOUNT_ID}/dataSources
{
"displayName": "My API Data Source",
"primaryProductDataSource": {}
}
答案
{
"name": "accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}",
"dataSourceId": "{DATASOURCE_ID}",
"displayName": "My API Data Source",
"primaryProductDataSource": {},
"input": "API"
}
如需了解详情,请参阅 创建商品评价数据源。
创建商品评价
您可以使用 accounts.productreviews.insert 方法创建或更新商品评价。accounts.productreviews.insert 方法接受
productreview 资源和数据源名称作为输入。如果成功,该方法会返回新的或更新后的 productreview。如需创建商品评价,您必须
拥有
datasource.name。
请求的格式:
POST https://merchantapi.googleapis.com/reviews/v1alpha/{parent=accounts/{ACCOUNT_ID}/}productReviews:insert
以下示例请求说明了如何创建商品评价。
POST https://merchantapi.googleapis.com/reviews/v1alpha/accounts/{ACCOUNT_ID}/productReviews:insert?dataSource=accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}
productReviewId = 'my_product_review'
productReviewAttributes {
aggregatorName = 'aggregator_name'
subclientName = 'subclient_name'
publisherName = 'publisher_name'
publisherFavicon = 'https://www.google.com/favicon.ico'
reviewerId = 'reviewer_id'
reviewerIsAnonymous = false
reviewerUsername = 'reviewer_username'
reviewLanguage = 'en'
reviewCountry = 'US'
reviewTime = '2024-04-01T00:00:00Z'
title = 'Incredible product'
content = 'This is an incredible product.'
pros = ['pro1', 'pro2']
cons = ['con1', 'con2']
reviewLink = {
type = 'SINGLETON'
link = 'https://www.google.com'
}
reviewerImageLinks = ['https://www.google.com/reviewer.png']
minRating = 1
maxRating = 10
rating = 8.5
productNames = ['product_name']
productLinks = ['https://www.google.com/product']
asins = ['asin1', 'asin2']
gtins = ['gtin1', 'gtin2']
mpns = ['mpn1', 'mpn2']
skus = ['sku1', 'sku2']
brands = ['brand1', 'brand2']
isSpam = false
collectionMethod = 'POST_FULFILLMENT'
transactionId = 'transaction_id'
}
创建商品评价后,可能需要几分钟时间才能传播评价。
以下是一个示例,您可以使用它以异步方式插入多条商品评价:
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.protobuf.Timestamp;
import com.google.shopping.merchant.reviews.v1alpha.InsertProductReviewRequest;
import com.google.shopping.merchant.reviews.v1alpha.ProductReview;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewAttributes;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewAttributes.ReviewLink;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewAttributes.ReviewLink.Type;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceClient;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceSettings;
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 multiple product reviews asynchronously. */
public class InsertProductReviewsAsyncSample {
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();
}
// Returns a product review with a random ID.
private static ProductReview createProductReview(String accountId) {
// MAKE SURE YOU PASS AN ACTUAL PRODUCT REVIEW ID HERE.
String productReviewId = generateRandomString();
ProductReviewAttributes attributes =
ProductReviewAttributes.newBuilder()
.setTitle("Would not recommend!")
.setContent("Not fantastic.")
.setMinRating(1)
.setMaxRating(5)
.setRating(2)
.setReviewTime(Timestamp.newBuilder().setSeconds(123456789).build())
.addProductLinks("exampleproducturl.com")
.setReviewLink(
ReviewLink.newBuilder()
.setLink("examplereviewurl.com")
// The review page contains only this single review.
.setType(Type.SINGLETON)
.build())
.addGtins("9780007350896")
.addGtins("9780007350897")
.build();
return ProductReview.newBuilder()
.setProductReviewId(productReviewId)
.setProductReviewAttributes(attributes)
.build();
}
public static void asyncInsertProductReviews(String accountId, String dataSourceId)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
ProductReviewsServiceSettings productReviewsServiceSettings =
ProductReviewsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
try (ProductReviewsServiceClient productReviewsServiceClient =
ProductReviewsServiceClient.create(productReviewsServiceSettings)) {
// Arbitrarily creates five product reviews with random IDs.
List<InsertProductReviewRequest> requests = new ArrayList<>();
for (int i = 0; i < 5; i++) {
InsertProductReviewRequest request =
InsertProductReviewRequest.newBuilder()
.setParent(String.format("accounts/%s", accountId))
.setProductReview(createProductReview(accountId))
// Must be a product reviews data source. In other words, a data source whose "type"
// is ProductReviewDataSource.
.setDataSource(String.format("accounts/%s/dataSources/%s", accountId, dataSourceId))
.build();
requests.add(request);
}
// Inserts the product reviews.
List<ApiFuture<ProductReview>> futures =
requests.stream()
.map(
request ->
productReviewsServiceClient.insertProductReviewCallable().futureCall(request))
.collect(Collectors.toList());
// Creates callback to handle the responses when all are ready.
ApiFuture<List<ProductReview>> responses = ApiFutures.allAsList(futures);
ApiFutures.addCallback(
responses,
new ApiFutureCallback<List<ProductReview>>() {
@Override
public void onSuccess(List<ProductReview> results) {
System.out.println("Inserted product reviews 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();
asyncInsertProductReviews(config.getAccountId().toString(), "YOUR_DATA_SOURCE_ID");
}
}
检索商品评价
如需查看商品评价,请使用 accounts.productreviews.get。此方法是只读的。
它需要您的 accountId 和商品评价的 ID 作为名称字段的一部分。GET 方法会返回相应的商品评价资源。
GET https://merchantapi.googleapis.com/reviews/v1alpha/{name=accounts/{ACCOUNT_ID}/productReviews/*}
以下是一个示例,您可以使用它来检索商品评价:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.reviews.v1alpha.GetProductReviewRequest;
import com.google.shopping.merchant.reviews.v1alpha.ProductReview;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceClient;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to get a product review. */
public class GetProductReviewSample {
public static void getProductReview(String accountId, String productReviewId) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
ProductReviewsServiceSettings productReviewsServiceSettings =
ProductReviewsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
try (ProductReviewsServiceClient productReviewsServiceClient =
ProductReviewsServiceClient.create(productReviewsServiceSettings)) {
GetProductReviewRequest request =
GetProductReviewRequest.newBuilder()
.setName(String.format("accounts/%s/productReviews/%s", accountId, productReviewId))
.build();
System.out.println("Sending get product review request:");
ProductReview response = productReviewsServiceClient.getProductReview(request);
System.out.println("Product review retrieved successfully:");
System.out.println(response.getName());
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
String productReviewId = "YOUR_PRODUCT_REVIEW_ID";
getProductReview(config.getAccountId().toString(), productReviewId);
}
}
列出商品评价
您可以使用 productreviews.list 方法查看所有已创建的商品评价。
GET https://merchantapi.googleapis.com/reviews/v1alpha/{parent=accounts/{ACCOUNT_ID}}/productReviews
以下是一个示例,您可以使用它列出商品的所有评价:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.reviews.v1alpha.ListProductReviewsRequest;
import com.google.shopping.merchant.reviews.v1alpha.ProductReview;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceClient;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceClient.ListProductReviewsPagedResponse;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to list all the product reviews in a given account. */
public class ListProductReviewsSample {
public static void listProductReviews(String accountId) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
ProductReviewsServiceSettings productReviewsServiceSettings =
ProductReviewsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
try (ProductReviewsServiceClient productReviewsServiceClient =
ProductReviewsServiceClient.create(productReviewsServiceSettings)) {
ListProductReviewsRequest request =
ListProductReviewsRequest.newBuilder()
.setParent(String.format("accounts/%s", accountId))
.build();
System.out.println("Sending list product reviews request:");
ListProductReviewsPagedResponse response =
productReviewsServiceClient.listProductReviews(request);
int count = 0;
// Iterates over all rows in all pages and prints all product reviews.
for (ProductReview element : response.iterateAll()) {
System.out.println(element);
count++;
}
System.out.print("The following count of elements were returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listProductReviews(config.getAccountId().toString());
}
}
删除商品评价
如需删除商品评价,请使用 accounts.productreviews.delete。与 GET 方法类似,此方法需要创建期间返回的商品评价的名称字段。
DELETE https://merchantapi.googleapis.com/reviews/v1alpha/{name=accounts/{ACCOUNT_ID}/productReviews/*}
以下是一个示例,您可以使用它来删除商品评价:
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.reviews.v1alpha.DeleteProductReviewRequest;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceClient;
import com.google.shopping.merchant.reviews.v1alpha.ProductReviewsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a product review. */
public class DeleteProductReviewSample {
public static void deleteProductReview(String accountId, String productReviewId)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
ProductReviewsServiceSettings productReviewsServiceSettings =
ProductReviewsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
try (ProductReviewsServiceClient productReviewsServiceClient =
ProductReviewsServiceClient.create(productReviewsServiceSettings)) {
DeleteProductReviewRequest request =
DeleteProductReviewRequest.newBuilder()
.setName(String.format("accounts/%s/productReviews/%s", accountId, productReviewId))
.build();
System.out.println("Sending delete product review request:");
productReviewsServiceClient.deleteProductReview(request);
System.out.println("Product review deleted successfully");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
String productReviewId = "YOUR_PRODUCT_REVIEW_ID";
deleteProductReview(config.getAccountId().toString(), productReviewId);
}
}
商品评价状态
商品评价资源包含与其他 API 类似的状态,这是资源的组成部分,并遵循相同的问题和目标结构。