Merchant API Code Sample to List Notification Subscriptions
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.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.rpc.ApiException;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.notifications.v1beta.ListNotificationSubscriptionsRequest;
import com.google.shopping.merchant.notifications.v1beta.NotificationSubscription;
import com.google.shopping.merchant.notifications.v1beta.NotificationsApiServiceClient;
import com.google.shopping.merchant.notifications.v1beta.NotificationsApiServiceClient.ListNotificationSubscriptionsPagedResponse;
import com.google.shopping.merchant.notifications.v1beta.NotificationsApiServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to list NotificationSubscriptions for a given Merchant Center
* account.
*/
public class ListNotificationSubscriptionsSample {
public static void listNotificationSubscriptions(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();
// Calls the API and catches and prints any network failures/errors.
try (NotificationsApiServiceClient notificationsApiServiceClient =
NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {
// The parent has the format: accounts/{account}
String parent = "accounts/" + config.getAccountId().toString();
ListNotificationSubscriptionsRequest request =
ListNotificationSubscriptionsRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list Notification Subscriptions request:");
ListNotificationSubscriptionsPagedResponse response =
notificationsApiServiceClient.listNotificationSubscriptions(request);
int count = 0;
for (NotificationSubscription notificationSubscription : response.iterateAll()) {
System.out.println(notificationSubscription);
count++;
}
System.out.print("The following count of notification subscriptions were returned: ");
System.out.println(count);
} catch (ApiException e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listNotificationSubscriptions(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\V1beta\Client\NotificationsApiServiceClient;
use Google\Shopping\Merchant\Notifications\V1beta\ListNotificationSubscriptionsRequest;
/**
* Lists Notification Subscriptions.
*/
class ListNotificationSubscriptions
{
/**
* Helper function to construct the parent resource name.
* @param $accountId
* The Merchant Center account ID.
* @return string
* The parent resource name.
*/
private static function getParent($accountId)
{
return "accounts/" . $accountId;
}
/**
* Lists Notification Subscriptions for a given Merchant Center account.
*
* @param array $config
* Configuration data for authentication and account details.
* @throws ApiException
*/
public static function listNotificationSubscriptionsSample($config): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Set up client options.
$options = ['credentials' => $credentials];
// Create a client instance.
$notificationsApiServiceClient = new NotificationsApiServiceClient($options);
// Construct the parent resource name.
$parent = self::getParent($config['accountId']);
// Create the request object.
$request = new ListNotificationSubscriptionsRequest(['parent' => $parent]);
print "Sending list Notification Subscriptions request:\n";
// Make the API call.
try {
$response = $notificationsApiServiceClient->listNotificationSubscriptions($request);
$count = 0;
foreach ($response->iterateAllElements() as $subscription) {
print_r($subscription);
$count++;
}
print "The following count of notification subscriptions were returned: " . $count . "\n";
} catch (ApiException $e) {
print "Request failed:\n";
print $e->getMessage() . "\n";
}
}
/**
* Execute the sample.
*
* @throws ApiException
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::listNotificationSubscriptionsSample($config);
}
}
// Run the script.
$sample = new ListNotificationSubscriptions();
$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 list NotificationSubscriptions."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_notifications_v1beta import ListNotificationSubscriptionsRequest
from google.shopping.merchant_notifications_v1beta import NotificationsApiServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"
def list_notification_subscriptions():
"""Lists NotificationSubscriptions for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = NotificationsApiServiceClient(credentials=credentials)
# Creates the request.
request = ListNotificationSubscriptionsRequest(parent=_PARENT)
print("Sending list Notification Subscriptions request:")
# Makes the request and catches and prints any error messages.
try:
response = client.list_notification_subscriptions(request=request)
count = 0
for notification_subscription in response:
print(notification_subscription)
count += 1
print(
"The following count of notification subscriptions were returned:"
f" {count}"
)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
list_notification_subscriptions()