获取商家信息
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用于获取商家信息的 Merchant API 代码示例。
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()
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis webpage provides code samples in Java and Python demonstrating how to retrieve business information from a Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe Java code utilizes the \u003ccode\u003eBusinessInfoServiceClient\u003c/code\u003e to send a \u003ccode\u003eGetBusinessInfoRequest\u003c/code\u003e and receive the business information, handling credential authentication and service settings creation.\u003c/p\u003e\n"],["\u003cp\u003eThe Python sample similarly employs the \u003ccode\u003eBusinessInfoServiceClient\u003c/code\u003e to retrieve business info, including the creation of user credentials and building the request to interact with the API.\u003c/p\u003e\n"],["\u003cp\u003eBoth the Java and Python examples showcase how to format the request \u003ccode\u003ename\u003c/code\u003e properly to target a specific Merchant Center account's business information.\u003c/p\u003e\n"]]],["The code samples demonstrate retrieving a Merchant Center account's business information using the Merchant API in Java, PHP, and Python. Each sample authenticates using OAuth credentials, then creates a `BusinessInfoServiceClient`. They construct a `BusinessInfo` name using the account ID, format: `accounts/{account}/businessInfo`. A `GetBusinessInfoRequest` is built using this name. Finally, each sends this request via the API, receiving and printing the business information as a response and catching potential errors.\n"],null,["# Get business info\n\nMerchant API code sample to get business info. \n\n### Java\n\n // Copyright 2024 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.accounts.businessinfos.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.BusinessInfo;\n import com.google.shopping.merchant.accounts.v1.BusinessInfoName;\n import com.google.shopping.merchant.accounts.v1.BusinessInfoServiceClient;\n import com.google.shopping.merchant.accounts.v1.BusinessInfoServiceSettings;\n import com.google.shopping.merchant.accounts.v1.GetBusinessInfoRequest;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to get the business info of a Merchant Center account. */\n public class GetBusinessInfoSample {\n\n public static void getBusinessInfo(Config config) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n BusinessInfoServiceSettings businessInfoServiceSettings =\n BusinessInfoServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates BusinessInfo name to identify the BusinessInfo.\n String name =\n BusinessInfoName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .build()\n .toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (BusinessInfoServiceClient businessInfoServiceClient =\n BusinessInfoServiceClient.create(businessInfoServiceSettings)) {\n\n // The name has the format: accounts/{account}/businessInfo\n GetBusinessInfoRequest request = GetBusinessInfoRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending get BusinessInfo request:\");\n BusinessInfo response = businessInfoServiceClient.getBusinessInfo(request);\n\n System.out.println(\"Retrieved BusinessInfo below\");\n System.out.println(response);\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n\n getBusinessInfo(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/businessinfos/v1/GetBusinessInfoSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n require_once __DIR__ . '/../../../../vendor/autoload.php';\n require_once __DIR__ . '/../../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\BusinessInfoServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\GetBusinessInfoRequest;\n\n /**\n * This class demonstrates how to get the business info of a Merchant Center account.\n */\n class GetBusinessInfoSample\n {\n /**\n * Gets the business info of a Merchant Center account.\n *\n * @param array $config\n * The configuration data used for authentication and getting the account ID.\n *\n * @return void\n */\n public static function getBusinessInfoSample(array $config): void\n {\n // Gets the OAuth credentials to make the request.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Creates options config containing credentials for the client to use.\n $options = ['credentials' =\u003e $credentials];\n\n // Creates a client.\n $businessInfoServiceClient = new BusinessInfoServiceClient($options);\n\n // Creates BusinessInfo name to identify the BusinessInfo.\n // The name has the format: accounts/{account}/businessInfo\n $name = \"accounts/\" . $config['accountId'] . \"/businessInfo\";\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n $request = new GetBusinessInfoRequest(['name' =\u003e $name]);\n print \"Sending get BusinessInfo request:\\n\";\n $response = $businessInfoServiceClient-\u003egetBusinessInfo($request);\n\n print \"Retrieved BusinessInfo below\\n\";\n print_r($response);\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n /**\n * Helper to execute the sample.\n *\n * @return void\n */\n public function callSample(): void\n {\n $config = Config::generateConfig();\n self::getBusinessInfoSample($config);\n }\n }\n\n // Run the script\n $sample = new GetBusinessInfoSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/businessinfos/v1/GetBusinessInfoSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2024 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \"\"\"A module to get BusinessInfo.\"\"\"\n\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import BusinessInfoServiceClient\n from google.shopping.merchant_accounts_v1 import GetBusinessInfoRequest\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def get_business_info():\n \"\"\"Gets the business information of a Merchant Center account.\"\"\"\n\n # Get OAuth credentials\n credentials = generate_user_credentials.main()\n\n # Create a BusinessInfoServiceClient\n business_info_service_client = BusinessInfoServiceClient(\n credentials=credentials\n )\n\n # Create BusinessInfo name\n name = \"accounts/\" + _ACCOUNT + \"/businessInfo\"\n\n # Create the request\n request = GetBusinessInfoRequest(name=name)\n\n # Call the API and print the response\n try:\n print(\"Sending get BusinessInfo request:\")\n response = business_info_service_client.get_business_info(request=request)\n print(\"Retrieved BusinessInfo below\")\n print(response)\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n get_business_info()\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/businessinfos/v1/get_business_info_sample.py"]]