Merchant API code sample to approve 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.AccountService
;
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.ApproveAccountServiceRequest
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to approve an account service. */
public
class
ApproveAccountServiceSample
{
public
static
void
approveAccountService
(
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}
ApproveAccountServiceRequest
request
=
ApproveAccountServiceRequest
.
newBuilder
().
setName
(
name
).
build
();
System
.
out
.
println
(
"Sending Approve Account Service request:"
);
AccountService
response
=
accountServicesServiceClient
.
approveAccountService
(
request
);
System
.
out
.
println
(
"Approved Account Service below"
);
System
.
out
.
println
(
response
);
}
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 approve (e.g. from a previous list
// call).
String
service
=
"111~222~333"
;
approveAccountService
(
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\ApproveAccountServiceRequest;
use Google\Shopping\Merchant\Accounts\V1\Client\AccountServicesServiceClient;
/**
* This class demonstrates how to approve an account service.
*/
class ApproveAccountServiceSample
{
/**
* 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 approved.
*
* @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);
}
/**
* Approves an account service.
*
* @param array $config The configuration data used for authentication and
* getting the account ID.
* @param string $service The service to be approved.
*/
public static function approveAccountService(
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 ApproveAccountServiceRequest())
->setName($name);
print "Sending Approve Account Service request:\n";
$response = $accountServicesServiceClient->approveAccountService($request);
printf("Approved Account Service below:%s", PHP_EOL);
print $response->serializeToJsonString(true) . PHP_EOL;
} 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 approve
// (e.g. from a previous list call).
$service = '111~222~333';
self::approveAccountService($config, $service);
}
}
// Run the script
$sample = new ApproveAccountServiceSample();
$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 approve 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
ApproveAccountServiceRequest
_ACCOUNT
=
configuration
.
Configuration
()
.
read_merchant_info
()
def
approve_account_service
(
service
:
str
)
-
> None
:
"""Approves an account service.
Args:
service: The service to approve. 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
=
ApproveAccountServiceRequest
(
name
=
name
)
# Makes the request and catches and prints any error messages.
try
:
print
(
"Sending Approve Account Service request:"
)
response
=
client
.
approve_account_service
(
request
=
request
)
print
(
"Approved Account Service below"
)
print
(
response
)
except
RuntimeError
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
# Update this with the name of the service you want to approve (e.g. from a
# previous list call).
service_
=
"111~222~333"
approve_account_service
(
service_
)