刪除通知訂閱項目
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Merchant API 程式碼範例,用於刪除通知訂閱項目。
Java
// Copyright 2025 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.notifications.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.notifications.v1.DeleteNotificationSubscriptionRequest;
import com.google.shopping.merchant.notifications.v1.NotificationSubscriptionName;
import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient;
import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to delete a specific Notification Subscription for a given Merchant
* Center account.
*/
public class DeleteNotificationSubscriptionSample {
public static void deleteNotificationSubscription(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
NotificationsApiServiceSettings notificationsApiServiceSettings =
NotificationsApiServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID. This must match
// the ID used when creating/getting the subscription.
String notificationSubscriptionId = "YOUR_NOTIFICATION_SUBSCRIPTION_ID";
// Creates notification subscription name to identify the subscription.
String name =
NotificationSubscriptionName.newBuilder()
.setAccount(config.getAccountId().toString())
.setNotificationSubscription(notificationSubscriptionId)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (NotificationsApiServiceClient notificationSubscriptionServiceClient =
NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {
// The name has the format:
// accounts/{account}/notificationSubscription/{notification_subscription_id}
DeleteNotificationSubscriptionRequest request =
DeleteNotificationSubscriptionRequest.newBuilder().setName(name).build();
System.out.println("Sending delete Notification Subscription request:");
notificationSubscriptionServiceClient.deleteNotificationSubscription(request);
System.out.println("Deleted Notification Subscription: " + name);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
deleteNotificationSubscription(config);
}
}
PHP
<?php
/**
* Copyright 2025 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\Shopping\Merchant\Notifications\V1\Client\NotificationsApiServiceClient;
use Google\Shopping\Merchant\Notifications\V1\DeleteNotificationSubscriptionRequest;
/**
* Deletes a Notification Subscription.
*/
class DeleteNotificationSubscription
{
/**
* Deletes a specific Notification Subscription for a given Merchant Center account.
*
* @param array $config
* Configuration data for authentication and account details.
* @param string $notificationSubscriptionId
* The ID of the notification subscription to delete.
* @throws ApiException
*/
public static function deleteNotificationSubscriptionSample($config, $notificationSubscriptionId): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Set up client options.
$options = ['credentials' => $credentials];
// Create a client instance.
$notificationsApiServiceClient = new NotificationsApiServiceClient($options);
// Construct the resource name.
$name = "accounts/" . $config['accountId'] .
"/notificationsubscriptions/" . $notificationSubscriptionId;
// Create the request object.
$request = new DeleteNotificationSubscriptionRequest(['name' => $name]);
print "Sending delete Notification Subscription request:\n";
// Make the API call.
try {
$notificationsApiServiceClient->deleteNotificationSubscription($request);
print "Deleted Notification Subscription: " . $name . "\n";
} catch (ApiException $e) {
print "Request failed:\n";
print $e->getMessage() . "\n";
}
}
/**
* Execute the sample.
*
* @throws ApiException
*/
public function callSample(): void
{
$config = Config::generateConfig();
// Replace with the actual notification subscription ID.
$notificationSubscriptionId = "YOUR_NOTIFICATION_SUBSCRIPTION_ID";
self::deleteNotificationSubscriptionSample($config, $notificationSubscriptionId);
}
}
// Run the script.
$sample = new DeleteNotificationSubscription();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# Copyright 2025 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 NotificationSubscription."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_notifications_v1 import DeleteNotificationSubscriptionRequest
from google.shopping.merchant_notifications_v1 import NotificationsApiServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def delete_notification_subscription():
"""Deletes a specific Notification Subscription for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = NotificationsApiServiceClient(credentials=credentials)
# Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID.
notification_subscription_id = "YOUR_NOTIFICATION_SUBSCRIPTION_ID"
# Creates notification subscription name to identify the subscription.
name = f"accounts/{_ACCOUNT}/notificationsubscriptions/{notification_subscription_id}"
# Creates the request.
request = DeleteNotificationSubscriptionRequest(name=name)
print("Sending delete Notification Subscription request:")
# Makes the request and catches and prints any error messages.
try:
client.delete_notification_subscription(request=request)
print(f"Deleted Notification Subscription: {name}")
except RuntimeError as e:
print(e)
if __name__ == "__main__":
delete_notification_subscription()
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[],[],null,["# Delete a notification subscription\n\nMerchant API code sample to delete a notification subscription. \n\n### Java\n\n // Copyright 2025 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.notifications.v1;\n\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.notifications.v1.DeleteNotificationSubscriptionRequest;\n import com.google.shopping.merchant.notifications.v1.NotificationSubscriptionName;\n import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient;\n import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /**\n * This class demonstrates how to delete a specific Notification Subscription for a given Merchant\n * Center account.\n */\n public class DeleteNotificationSubscriptionSample {\n\n public static void deleteNotificationSubscription(Config config) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n NotificationsApiServiceSettings notificationsApiServiceSettings =\n NotificationsApiServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Replace \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\" with the actual ID. This must match\n // the ID used when creating/getting the subscription.\n String notificationSubscriptionId = \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\";\n\n // Creates notification subscription name to identify the subscription.\n String name =\n NotificationSubscriptionName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setNotificationSubscription(notificationSubscriptionId)\n .build()\n .toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (NotificationsApiServiceClient notificationSubscriptionServiceClient =\n NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {\n\n // The name has the format:\n // accounts/{account}/notificationSubscription/{notification_subscription_id}\n DeleteNotificationSubscriptionRequest request =\n DeleteNotificationSubscriptionRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending delete Notification Subscription request:\");\n notificationSubscriptionServiceClient.deleteNotificationSubscription(request);\n\n System.out.println(\"Deleted Notification Subscription: \" + name);\n\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n deleteNotificationSubscription(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/notifications/v1/DeleteNotificationSubscriptionSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 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\n require_once __DIR__ . '/../../../vendor/autoload.php';\n require_once __DIR__ . '/../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\Notifications\\V1\\Client\\NotificationsApiServiceClient;\n use Google\\Shopping\\Merchant\\Notifications\\V1\\DeleteNotificationSubscriptionRequest;\n\n /**\n * Deletes a Notification Subscription.\n */\n class DeleteNotificationSubscription\n {\n /**\n * Deletes a specific Notification Subscription for a given Merchant Center account.\n *\n * @param array $config\n * Configuration data for authentication and account details.\n * @param string $notificationSubscriptionId\n * The ID of the notification subscription to delete.\n * @throws ApiException\n */\n public static function deleteNotificationSubscriptionSample($config, $notificationSubscriptionId): void\n {\n // Get OAuth credentials.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Set up client options.\n $options = ['credentials' =\u003e $credentials];\n\n // Create a client instance.\n $notificationsApiServiceClient = new NotificationsApiServiceClient($options);\n\n // Construct the resource name.\n $name = \"accounts/\" . $config['accountId'] .\n \"/notificationsubscriptions/\" . $notificationSubscriptionId;\n\n // Create the request object.\n $request = new DeleteNotificationSubscriptionRequest(['name' =\u003e $name]);\n\n print \"Sending delete Notification Subscription request:\\n\";\n\n // Make the API call.\n try {\n $notificationsApiServiceClient-\u003edeleteNotificationSubscription($request);\n print \"Deleted Notification Subscription: \" . $name . \"\\n\";\n } catch (ApiException $e) {\n print \"Request failed:\\n\";\n print $e-\u003egetMessage() . \"\\n\";\n }\n }\n\n /**\n * Execute the sample.\n *\n * @throws ApiException\n */\n public function callSample(): void\n {\n $config = Config::generateConfig();\n // Replace with the actual notification subscription ID.\n $notificationSubscriptionId = \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\";\n\n self::deleteNotificationSubscriptionSample($config, $notificationSubscriptionId);\n }\n }\n\n // Run the script.\n $sample = new DeleteNotificationSubscription();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/notifications/v1/DeleteNotificationSubscriptionSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2025 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 # http://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 \"\"\"A module to delete NotificationSubscription.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_notifications_v1 import DeleteNotificationSubscriptionRequest\n from google.shopping.merchant_notifications_v1 import NotificationsApiServiceClient\n\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def delete_notification_subscription():\n \"\"\"Deletes a specific Notification Subscription for a given Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = NotificationsApiServiceClient(credentials=credentials)\n\n # Replace \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\" with the actual ID.\n notification_subscription_id = \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\"\n\n # Creates notification subscription name to identify the subscription.\n name = f\"accounts/{_ACCOUNT}/notificationsubscriptions/{notification_subscription_id}\"\n\n # Creates the request.\n request = DeleteNotificationSubscriptionRequest(name=name)\n\n print(\"Sending delete Notification Subscription request:\")\n # Makes the request and catches and prints any error messages.\n try:\n client.delete_notification_subscription(request=request)\n print(f\"Deleted Notification Subscription: {name}\")\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n delete_notification_subscription() \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/notifications/v1/delete_notification_subscription_sample.py"]]