Page Summary
-
This webpage provides code samples in Java, PHP, and Python demonstrating how to retrieve shipping settings for a specific Merchant Center account using the Merchant API.
-
The code samples utilize the
ShippingSettingsServiceClientto make the request to thegetShippingSettingsmethod. -
Each code sample generates an OAuth credential to authorize API calls and constructs a
GetShippingSettingsRequestwith the appropriate account name. -
The samples showcase error handling to manage exceptions during API calls, like network failures and errors.
-
The provided code includes proper license and copyright information, and has links to the relevant section of the Github repo.
Merchant API code sample to get a shipping settings.
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.shippingsettings.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.accounts.v1.GetShippingSettingsRequest
;
import
com.google.shopping.merchant.accounts.v1.ShippingSettings
;
import
com.google.shopping.merchant.accounts.v1.ShippingSettingsName
;
import
com.google.shopping.merchant.accounts.v1.ShippingSettingsServiceClient
;
import
com.google.shopping.merchant.accounts.v1.ShippingSettingsServiceSettings
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to get the ShippingSettings for a given Merchant Center account. */
public
class
GetShippingSettingsSample
{
public
static
void
getShippingSettings
(
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.
ShippingSettingsServiceSettings
shippingSettingsServiceSettings
=
ShippingSettingsServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Creates ShippingSettings name to identify ShippingSettings.
String
name
=
ShippingSettingsName
.
newBuilder
()
.
setAccount
(
config
.
getAccountId
().
toString
())
.
build
()
.
toString
();
// Calls the API and catches and prints any network failures/errors.
try
(
ShippingSettingsServiceClient
shippingSettingsServiceClient
=
ShippingSettingsServiceClient
.
create
(
shippingSettingsServiceSettings
))
{
// The name has the format: accounts/{account}/shippingSettings
GetShippingSettingsRequest
request
=
GetShippingSettingsRequest
.
newBuilder
().
setName
(
name
).
build
();
System
.
out
.
println
(
"Sending Get ShippingSettings request:"
);
ShippingSettings
response
=
shippingSettingsServiceClient
.
getShippingSettings
(
request
);
System
.
out
.
println
(
"Retrieved ShippingSettings below"
);
System
.
out
.
println
(
response
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
getShippingSettings
(
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\ShippingSettingsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\GetShippingSettingsRequest;
/**
* This class demonstrates how to get the ShippingSettings for a given Merchant Center account.
*/
class GetShippingSettings
{
/**
* Retrieves the shipping settings for the specified Merchant Center account.
*
* @param array $config The configuration data containing the account ID.
* @return void
*/
public static function getShippingSettings($config)
{
// Gets the OAuth credentials to make the request.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options config containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$shippingSettingsServiceClient = new ShippingSettingsServiceClient($options);
// Creates ShippingSettings name to identify ShippingSettings.
// The name has the format: accounts/{account}/shippingSettings
$name = "accounts/" . $config['accountId'] . "/shippingSettings";
// Calls the API and catches and prints any network failures/errors.
try {
$request = (new GetShippingSettingsRequest())
->setName($name);
print "Sending Get ShippingSettings request:" . PHP_EOL;
$response = $shippingSettingsServiceClient->getShippingSettings($request);
print "Retrieved ShippingSettings below" . PHP_EOL;
print_r($response);
} catch (ApiException $e) {
print $e->getMessage();
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
// Makes the call to get shipping settings for the MC account.
self::getShippingSettings($config);
}
}
// Run the script
$sample = new GetShippingSettings();
$sample->callSample();

