Merchant API code sample to reject an account service.
Java
// 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.accounts.accountservices.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.AccountServiceName;
import com.google.shopping.merchant.accounts.v1.AccountServicesServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountServicesServiceSettings;
import com.google.shopping.merchant.accounts.v1.RejectAccountServiceRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to reject an account service. */
public class RejectAccountServiceSample {
public static void rejectAccountService(Config config, String service) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
AccountServicesServiceSettings accountServicesServiceSettings =
AccountServicesServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Gets the account ID from the config file.
String accountId = config.getAccountId().toString();
// Creates account service name to identify the account service.
String name =
AccountServiceName.newBuilder()
.setAccount(accountId)
.setService(service)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (AccountServicesServiceClient accountServicesServiceClient =
AccountServicesServiceClient.create(accountServicesServiceSettings)) {
// The name has the format: accounts/{account}/services/{provider}
RejectAccountServiceRequest request =
RejectAccountServiceRequest.newBuilder().setName(name).build();
System.out.println("Sending Reject Account Service request:");
accountServicesServiceClient.rejectAccountService(request);
System.out.println("Successfully rejected account service");
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// Update this with the name of the service you want to reject (e.g. from a previous list call).
String service = "111~222~333";
rejectAccountService(config, service);
}
}
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\Accounts\V1\Client\AccountServicesServiceClient;
use Google\Shopping\Merchant\Accounts\V1\RejectAccountServiceRequest;
/**
* This class demonstrates how to reject an account service.
*/
class RejectAccountServiceSample
{
/**
* A helper function to create the name of the service.
*
* @param string $accountId The account that owns the product.
* @param string $service The service to be rejected.
*
* @return string The name has the format:
* `accounts/{account}/services/{service}`
*/
private static function getName(string $accountId, string $service): string
{
return sprintf('accounts/%s/services/%s', $accountId, $service);
}
/**
* Rejects an account service.
*
* @param array $config The configuration data used for authentication and
* getting the account ID.
* @param string $service The service to be rejected.
*/
public static function rejectAccountService(
array $config,
string $service
): void {
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$accountServicesServiceClient = new AccountServicesServiceClient($options);
// Gets the account ID from the config file.
$accountId = $config['accountId'];
// Creates account service name to identify the account service.
$name = self::getName($accountId, $service);
// Calls the API and catches and prints any network failures/errors.
try {
// The name has the format: accounts/{account}/services/{provider}
$request = (new RejectAccountServiceRequest())
->setName($name);
print "Sending Reject Account Service request:\n";
$accountServicesServiceClient->rejectAccountService($request);
print "Successfully rejected account service\n";
} catch (ApiException $e) {
printf("An error has occurred: %s%s", $e->getMessage(), PHP_EOL);
}
}
/**
* Helper to execute the sample.
*/
public function callSample(): void
{
$config = Config::generateConfig();
// Update this with the name of the service you want to reject
// (e.g. from a previous list call).
$service = '111~222~333';
self::rejectAccountService($config, $service);
}
}
// Run the script
$sample = new RejectAccountServiceSample();
$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.
"""This class demonstrates how to reject an account service."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import AccountServicesServiceClient
from google.shopping.merchant_accounts_v1 import RejectAccountServiceRequest
_ACCOUNT = configuration.Configuration().read_merchant_info()
def reject_account_service(service: str) -> None:
"""Rejects an account service.
Args:
service: The service to reject. This is the ID of the service, not the full
resource name.
"""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = AccountServicesServiceClient(credentials=credentials)
# The name has the format: accounts/{account}/services/{provider}
name = f"accounts/{_ACCOUNT}/services/{service}"
# Creates the request.
request = RejectAccountServiceRequest(name=name)
# Makes the request and catches and prints any error messages.
try:
print("Sending Reject Account Service request:")
client.reject_account_service(request=request)
print("Successfully rejected account service")
except RuntimeError as e:
print(e)
if __name__ == "__main__":
# Update this with the name of the service you want to reject (e.g. from a
# previous list call).
service_ = "111~222~333"
reject_account_service(service_)