Merchant API code sample to get automatic improvements.
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.automaticimprovements.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.accounts.v1.AutomaticImprovements
;
import
com.google.shopping.merchant.accounts.v1.AutomaticImprovementsName
;
import
com.google.shopping.merchant.accounts.v1.AutomaticImprovementsServiceClient
;
import
com.google.shopping.merchant.accounts.v1.AutomaticImprovementsServiceSettings
;
import
com.google.shopping.merchant.accounts.v1.GetAutomaticImprovementsRequest
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to get the automatic improvements of a Merchant Center account. */
public
class
GetAutomaticImprovementsSample
{
public
static
void
getAutomaticImprovements
(
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.
AutomaticImprovementsServiceSettings
automaticImprovementsServiceSettings
=
AutomaticImprovementsServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Creates AutomaticImprovements name to identify the AutomaticImprovements.
String
name
=
AutomaticImprovementsName
.
newBuilder
()
.
setAccount
(
config
.
getAccountId
().
toString
())
.
build
()
.
toString
();
// Calls the API and catches and prints any network failures/errors.
try
(
AutomaticImprovementsServiceClient
automaticImprovementsServiceClient
=
AutomaticImprovementsServiceClient
.
create
(
automaticImprovementsServiceSettings
))
{
// The name has the format: accounts/{account}/automaticImprovements
GetAutomaticImprovementsRequest
request
=
GetAutomaticImprovementsRequest
.
newBuilder
().
setName
(
name
).
build
();
System
.
out
.
println
(
"Sending get AutomaticImprovements request:"
);
AutomaticImprovements
response
=
automaticImprovementsServiceClient
.
getAutomaticImprovements
(
request
);
System
.
out
.
println
(
"Retrieved AutomaticImprovements below"
);
System
.
out
.
println
(
response
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
getAutomaticImprovements
(
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\Accounts\V1\Client\AutomaticImprovementsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\GetAutomaticImprovementsRequest;
/**
* This class demonstrates how to get the automatic improvements of a Merchant Center account.
*/
class GetAutomaticImprovementsSample
{
/**
* Helper function to construct the resource name for AutomaticImprovements.
*
* @param string $accountId The Merchant Center account ID.
* @return string The resource name in the format: accounts/{account}/automaticImprovements
*/
private static function getAutomaticImprovementsName(string $accountId): string
{
return sprintf("accounts/%s/automaticImprovements", $accountId);
}
/**
* Retrieves the automatic improvements settings for a given Merchant Center account.
*
* @param array $config The configuration array containing the account ID.
* @return void
*/
public static function getAutomaticImprovementsSample(array $config): void
{
// Obtains OAuth credentials for authentication.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Contructs an options array for the client.
$options = ['credentials' => $credentials];
// Creates a new AutomaticImprovementsServiceClient.
$automaticImprovementsServiceClient = new AutomaticImprovementsServiceClient($options);
// Constructs the full resource name for the automatic improvements settings.
$name = self::getAutomaticImprovementsName($config['accountId']);
// Creates the GetAutomaticImprovementsRequest.
$request = new GetAutomaticImprovementsRequest(['name' => $name]);
printf("Sending get AutomaticImprovements request:%s", PHP_EOL);
try {
// Makes the API call to retrieve automatic improvements settings.
$response = $automaticImprovementsServiceClient->getAutomaticImprovements($request);
printf("Retrieved AutomaticImprovements below%s", PHP_EOL);
// Prints the response in JSON format for readability.
print_r($response);
} catch (ApiException $e) {
printf("ApiException was thrown: %s%s", $e->getMessage(), PHP_EOL);
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::getAutomaticImprovementsSample($config);
}
}
// Runs the script.
$sample = new GetAutomaticImprovementsSample();
$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.
"""Gets the automatic improvements settings for a Merchant Center account."""
from
examples.authentication
import
configuration
from
examples.authentication
import
generate_user_credentials
from
google.shopping.merchant_accounts_v1
import
AutomaticImprovementsServiceClient
from
google.shopping.merchant_accounts_v1
import
GetAutomaticImprovementsRequest
# Fetches the account ID from the config file.
# This is a placeholder for your actual account ID.
_ACCOUNT_ID
=
configuration
.
Configuration
()
.
read_merchant_info
()
# Construct the resource name for AutomaticImprovements.
# The format is accounts/{account}/automaticImprovements
_NAME
=
f
"accounts/
{
_ACCOUNT_ID
}
/automaticImprovements"
def
get_automatic_improvements_sample
():
"""Gets the automatic improvements settings for a Merchant Center account."""
# Generates OAuth 2.0 credentials for authentication.
credentials
=
generate_user_credentials
.
main
()
# Creates a client for the AutomaticImprovementsService.
client
=
AutomaticImprovementsServiceClient
(
credentials
=
credentials
)
# Creates the request to get automatic improvements.
# The name parameter is the resource name of the automatic improvements
# settings.
request
=
GetAutomaticImprovementsRequest
(
name
=
_NAME
)
print
(
"Sending get AutomaticImprovements request:"
)
# Makes the API request to get automatic improvements.
try
:
response
=
client
.
get_automatic_improvements
(
request
=
request
)
print
(
"Retrieved AutomaticImprovements below"
)
print
(
response
)
except
RuntimeError
as
e
:
print
(
f
"An API error occurred:
{
e
}
"
)
if
__name__
==
"__main__"
:
get_automatic_improvements_sample
()