Page Summary
-
This webpage provides code samples in Java and Python demonstrating how to retrieve business information from a Merchant Center account using the Merchant API.
-
The Java code utilizes the
BusinessInfoServiceClientto send aGetBusinessInfoRequestand receive the business information, handling credential authentication and service settings creation. -
The Python sample similarly employs the
BusinessInfoServiceClientto retrieve business info, including the creation of user credentials and building the request to interact with the API. -
Both the Java and Python examples showcase how to format the request
nameproperly to target a specific Merchant Center account's business information.
Merchant API code sample to get business info.
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.businessinfos.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.accounts.v1.BusinessInfo
;
import
com.google.shopping.merchant.accounts.v1.BusinessInfoName
;
import
com.google.shopping.merchant.accounts.v1.BusinessInfoServiceClient
;
import
com.google.shopping.merchant.accounts.v1.BusinessInfoServiceSettings
;
import
com.google.shopping.merchant.accounts.v1.GetBusinessInfoRequest
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to get the business info of a Merchant Center account. */
public
class
GetBusinessInfoSample
{
public
static
void
getBusinessInfo
(
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.
BusinessInfoServiceSettings
businessInfoServiceSettings
=
BusinessInfoServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Creates BusinessInfo name to identify the BusinessInfo.
String
name
=
BusinessInfoName
.
newBuilder
()
.
setAccount
(
config
.
getAccountId
().
toString
())
.
build
()
.
toString
();
// Calls the API and catches and prints any network failures/errors.
try
(
BusinessInfoServiceClient
businessInfoServiceClient
=
BusinessInfoServiceClient
.
create
(
businessInfoServiceSettings
))
{
// The name has the format: accounts/{account}/businessInfo
GetBusinessInfoRequest
request
=
GetBusinessInfoRequest
.
newBuilder
().
setName
(
name
).
build
();
System
.
out
.
println
(
"Sending get BusinessInfo request:"
);
BusinessInfo
response
=
businessInfoServiceClient
.
getBusinessInfo
(
request
);
System
.
out
.
println
(
"Retrieved BusinessInfo below"
);
System
.
out
.
println
(
response
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
getBusinessInfo
(
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\BusinessInfoServiceClient;
use Google\Shopping\Merchant\Accounts\V1\GetBusinessInfoRequest;
/**
* This class demonstrates how to get the business info of a Merchant Center account.
*/
class GetBusinessInfoSample
{
/**
* Gets the business info of a Merchant Center account.
*
* @param array $config
* The configuration data used for authentication and getting the account ID.
*
* @return void
*/
public static function getBusinessInfoSample(array $config): void
{
// 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.
$businessInfoServiceClient = new BusinessInfoServiceClient($options);
// Creates BusinessInfo name to identify the BusinessInfo.
// The name has the format: accounts/{account}/businessInfo
$name = "accounts/" . $config['accountId'] . "/businessInfo";
// Calls the API and catches and prints any network failures/errors.
try {
$request = new GetBusinessInfoRequest(['name' => $name]);
print "Sending get BusinessInfo request:\n";
$response = $businessInfoServiceClient->getBusinessInfo($request);
print "Retrieved BusinessInfo below\n";
print_r($response);
} catch (ApiException $e) {
print $e->getMessage();
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::getBusinessInfoSample($config);
}
}
// Run the script
$sample = new GetBusinessInfoSample();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# 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
#
# 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 get BusinessInfo."""
from
examples.authentication
import
configuration
from
examples.authentication
import
generate_user_credentials
from
google.shopping.merchant_accounts_v1
import
BusinessInfoServiceClient
from
google.shopping.merchant_accounts_v1
import
GetBusinessInfoRequest
_ACCOUNT
=
configuration
.
Configuration
()
.
read_merchant_info
()
def
get_business_info
():
"""Gets the business information of a Merchant Center account."""
# Get OAuth credentials
credentials
=
generate_user_credentials
.
main
()
# Create a BusinessInfoServiceClient
business_info_service_client
=
BusinessInfoServiceClient
(
credentials
=
credentials
)
# Create BusinessInfo name
name
=
"accounts/"
+
_ACCOUNT
+
"/businessInfo"
# Create the request
request
=
GetBusinessInfoRequest
(
name
=
name
)
# Call the API and print the response
try
:
print
(
"Sending get BusinessInfo request:"
)
response
=
business_info_service_client
.
get_business_info
(
request
=
request
)
print
(
"Retrieved BusinessInfo below"
)
print
(
response
)
except
RuntimeError
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
get_business_info
()

