用于删除商品输入的 Merchant API 代码示例
// Copyright 2024 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.products.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.products.v1beta.DeleteProductInputRequest;
import com.google.shopping.merchant.products.v1beta.ProductInputName;
import com.google.shopping.merchant.products.v1beta.ProductInputsServiceClient;
import com.google.shopping.merchant.products.v1beta.ProductInputsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a product for a given Merchant Center account */
public class DeleteProductInputSample {
public static void deleteProductInput(Config config, String productId, String dataSource)
throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
ProductInputsServiceSettings productInputsServiceSettings =
ProductInputsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates product name to identify product.
String name =
ProductInputName.newBuilder()
.setAccount(config.getAccountId().toString())
.setProductinput(productId)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (ProductInputsServiceClient productInputsServiceClient =
ProductInputsServiceClient.create(productInputsServiceSettings)) {
DeleteProductInputRequest request =
DeleteProductInputRequest.newBuilder().setName(name).setDataSource(dataSource).build();
System.out.println("Sending deleteProductInput request");
productInputsServiceClient.deleteProductInput(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. If you make a products.get or products.list request before a few"
+ " minutes have passed, the old product data may be returned.");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// An ID assigned to a product by Google. In the format
// channel~contentLanguage~feedLabel~offerId
String productId = "online~en~label~sku123";
// The name of the dataSource from which to delete the product. If it is a primary feed, this
// will delete the product completely. If it's a supplemental feed, it will only delete the
// product information from that feed, but the product will still be available from the primary
// feed.
String dataSource = "accounts/{account}/dataSources/{dataSource}";
deleteProductInput(config, productId, dataSource);
}
}
<?php
/**
* Copyright 2024 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.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/../../Authentication/Authentication.php';
require_once __DIR__ . '/../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Auth\CredentialsLoader;
use Google\Shopping\Merchant\Products\V1beta\Client\ProductInputsServiceClient;
use Google\Shopping\Merchant\Products\V1beta\DeleteProductInputRequest;
use Google\Shopping\Merchant\Products\V1beta\ProductInputName;
/**
* This class demonstrates how to delete a productinput for a given Merchant Center
* account.
*/
class DeleteProductInput
{
// ENSURE you fill in the product and datasource ID for the sample to work.
// An ID assigned to a product by Google:
// In the format `channel~contentLanguage~feedLabel~offerId`
private const PRODUCT = 'INSERT_PRODUCT_ID_HERE';
private const DATASOURCE = 'INSERT_DATASOURCE_ID_HERE';
/**
* Deletes a product input to your Merchant Center account.
*
* @param array $config
* The configuration data used for authentication and getting the
* acccount ID.
* @param string $product
* The product ID to delete.
* Format: `accounts/{account}/products/{productId}`.
* @param string $dataSource
* The primary or supplemental product data source name that owns the
* product.
* Format: `accounts/{account}/dataSources/{datasource}`.
*
* @return void
*/
public static function deleteProductInputSample(
$config, $product, $dataSource
): void {
// Obtains OAuth token based on the user's configuration.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates service settings using the credentials retrieved above.
$options = ['credentials' => $credentials];
$productInputsServiceClient = new ProductInputsServiceClient($options);
// Calls the API and catches and prints any network failures/errors.
try {
$request = new DeleteProductInputRequest(
[
'name' => $product,
'data_source' => $dataSource
]
);
echo "Sending deleteProductInput request\n";
$productInputsServiceClient->deleteProductInput($request);
echo "Delete successful, note that it may take a few minutes for the "
. "delete to update in the system. If you make a products.get or "
. "products.list request before a few minutes have passed, the old "
. "product data may be returned.\n";
} catch (ApiException $e) {
echo "An error has occurred: \n";
echo $e->getMessage() . "\n";
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
// The productID variable is defined at the top of the file.
$product = ProductInputsServiceClient::productInputName(
$config['accountId'],
self::PRODUCT
);
// The name of the dataSource from which to delete the product.
$dataSource = sprintf(
'accounts/%s/dataSources/%s',
$config['accountId'],
self::DATASOURCE
);
self::deleteProductInputSample($config, $product, $dataSource);
}
}
// Run the script.
$sample = new DeleteProductInput();
$sample->callSample();
# -*- coding: utf-8 -*-
# Copyright 2024 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
#
# http://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.
"""A module to delete a Product Input."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping import merchant_products_v1beta
_ACCOUNT = configuration.Configuration().read_merchant_info()
# ENSURE you fill in the product ID and data source for the
# sample to work.
# In the format of `channel~contentLanguage~feedLabel~offerId`
_PRODUCT = "[INSERT_PRODUCT_HERE]"
_DATA_SOURCE = "[INSERT_DATA_SOURCE_HERE]"
_NAME = f"accounts/{_ACCOUNT}/productInputs/{_PRODUCT}"
_DATA_SOURCE_NAME = f"accounts/{_ACCOUNT}/dataSources/{_DATA_SOURCE}"
def delete_product_input():
"""Deletes the specified `ProductInput` resource."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = merchant_products_v1beta.ProductInputsServiceClient(
credentials=credentials
)
# Creates the request.
request = merchant_products_v1beta.DeleteProductInputRequest(
name=_NAME, data_source=_DATA_SOURCE_NAME
)
# Makes the request and catch and print any error messages.
try:
client.delete_product_input(request=request)
print("Deletion successful")
except RuntimeError as e:
print("Deletion failed")
print(e)
if __name__ == "__main__":
delete_product_input()