private static ShoppingContent initializeContentService() throws IOException, GeneralSecurityException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
FileInputStream credentialsStream = new FileInputStream(GOOGLE_JSON_PATH);
GoogleCredentials googleCredentials = GoogleCredentials.fromStream(credentialsStream).createScoped(ShoppingContentScopes.all());
return new ShoppingContent
.Builder(httpTransport, jsonFactory, new HttpCredentialsAdapter(googleCredentials))
.setApplicationName("Merchant Center")
.build();
}
private static final String GOOGLE_JSON_PATH = "/gsdata/tokens/gs-jj-335906-808f447c80b6.json"; GOOGLE_JSON_PATH :密钥文件路径,gs-jj-335906-808f447c80b6.json创建服务账号Google生成密钥文件
Google服务帐号访问 Content API for Shopping 文档路径
1.创建产品
public static String sampleProductId(String contentLanguage, String targetCountry, String offerId) {
return CHANNEL + ":" + contentLanguage + ":" + targetCountry + ":" + offerId;
}
List<ProductShipping> productShippingList = new ArrayList<>();
ProductShipping productShipping = new ProductShipping();
productShipping.setCountry("US");
productShipping.setPrice(new Price().setValue("0.00").setCurrency("USD"));
productShippingList.add(productShipping);
Product product = new Product()
.setId(sampleProductId("en", "US", "111111"))
.setOfferId("111111")
.setKind("content#product")//固定为content#product
.setColor("custom colors")//颜色
.setSizes(Arrays.asList("one size"))//尺寸
.setTitle("title")
.setDescription("description")
.setGender("unisex")
.setGoogleProductCategory("Apparel & Accessories Handbag & Wallet Accessories Keychains")//google产品分类
.setGtin(StringUtils.isNotEmpty(customProduct.getGtin()) ? customProduct.getGtin() : "")
.setLink("https://" + url + customProduct.getProductRouting())
.setBrand("xxxx")
.setImageLink("product img")
.setChannel(CHANNEL)
.setContentLanguage("en")
.setTargetCountry("US")
.setAvailability("in stock")//库存,in stock表达有库存
.setCondition("new")
.setShipping(productShippingList)//多个国家,配置多个country设置不同
.setPrice(new Price().setValue("123").setCurrency("USD"));
ShoppingContent contentService = initializeContentService();
contentService.products().insert(MERCHANT_ID, product).execute();
private static final String CHANNEL = "online";
id规则:channel:contentLanguage:targetCountry:offerId