用于查找 LFP 提供商的 Merchant API 代码示例。
Java
// 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.
package shopping.merchant.samples.accounts.lfpproviders.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.FindLfpProvidersRequest;
import com.google.shopping.merchant.accounts.v1.LfpProvider;
import com.google.shopping.merchant.accounts.v1.LfpProvidersServiceClient;
import com.google.shopping.merchant.accounts.v1.LfpProvidersServiceClient.FindLfpProvidersPagedResponse;
import com.google.shopping.merchant.accounts.v1.LfpProvidersServiceSettings;
import com.google.shopping.merchant.accounts.v1.OmnichannelSettingName;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to get the Lfp Providers for a given Merchant Center account */
public class FindLfpProvidersSample {
public static void findLfpProviders(Config config, String regionCode)
throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
LfpProvidersServiceSettings lfpProvidersServiceSettings =
LfpProvidersServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Gets the account ID from the config file.
String accountId = config.getAccountId().toString();
// Creates parent to identify the omnichannelSetting from which to list all Lfp Providers.
String parent =
OmnichannelSettingName.newBuilder()
.setAccount(accountId)
.setOmnichannelSetting(regionCode)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (LfpProvidersServiceClient lfpProvidersServiceClient =
LfpProvidersServiceClient.create(lfpProvidersServiceSettings)) {
FindLfpProvidersRequest request =
FindLfpProvidersRequest.newBuilder().setParent(parent).build();
System.out.println("Sending find LFP providers request:");
FindLfpProvidersPagedResponse response = lfpProvidersServiceClient.findLfpProviders(request);
int count = 0;
// Iterates over all the entries in the response.
for (LfpProvider lfpProvider : response.iterateAll()) {
System.out.println(lfpProvider);
count++;
}
System.out.println(String.format("The following count of elements were returned: %d", count));
} catch (Exception e) {
System.out.println("An error has occured: ");
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The country you're targeting at.
String regionCode = "{REGION_CODE}";
findLfpProviders(config, regionCode);
}
}
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\LfpProvidersServiceClient;
use Google\Shopping\Merchant\Accounts\V1\FindLfpProvidersRequest;
use Google\Shopping\Merchant\Accounts\V1\LfpProvider;
/**
* This class demonstrates how to get the LFP Providers for a given Merchant
* Center account.
*/
class FindLfpProvidersSample
{
/**
* A helper function to create the parent string.
*
* @param string $accountId The Merchant Center account ID.
* @param string $regionCode The region code for the omnichannel setting.
*
* @return string The parent has the format:
* `accounts/{account}/omnichannelSettings/{omnichannelSetting}`
*/
private static function getParent(string $accountId, string $regionCode): string
{
return sprintf(
"accounts/%s/omnichannelSettings/%s",
$accountId,
$regionCode
);
}
/**
* Retrieves all LFP providers for a given account and region.
*
* @param array $config The configuration data for authentication.
* @param string $regionCode The CLDR country code of the target country.
*/
public static function findLfpProviders(
array $config,
string $regionCode
): 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.
$lfpProvidersServiceClient = new LfpProvidersServiceClient($options);
// Creates the parent resource name from the account ID and region code.
$parent = self::getParent($config['accountId'], $regionCode);
// Creates the request.
$request = (new FindLfpProvidersRequest())
->setParent($parent);
// Calls the API and catches and prints any network failures/errors.
try {
printf("Sending find LFP providers request:%s", PHP_EOL);
$response = $lfpProvidersServiceClient->findLfpProviders($request);
$count = 0;
// Iterates over all the LFP providers in the response and prints them.
foreach ($response->iterateAllElements() as $lfpProvider) {
// The LfpProvider object is a Protobuf message.
// We are printing it as a JSON string for readability.
print($lfpProvider->serializeToJsonString(true) . PHP_EOL);
$count++;
}
printf(
"The following count of elements were returned: %d%s",
$count,
PHP_EOL
);
} catch (ApiException $e) {
printf("An error has occured: %s%s", PHP_EOL, $e);
}
}
/**
* Helper to execute the sample.
*/
public static function callSample(): void
{
$config = Config::generateConfig();
// The country you're targeting.
$regionCode = '{REGION_CODE}';
self::findLfpProviders($config, $regionCode);
}
}
// Runs the sample.
FindLfpProvidersSample::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.
"""Sample for finding LFP providers for a given Merchant Center account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import FindLfpProvidersRequest
from google.shopping.merchant_accounts_v1 import LfpProvidersServiceClient
# Gets the merchant account ID from the configuration file.
_ACCOUNT = configuration.Configuration().read_merchant_info()
def find_lfp_providers(region_code: str) -> None:
"""Gets the LFP Providers for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = LfpProvidersServiceClient(credentials=credentials)
# The parent resource name of the omnichannel setting.
# Format: `accounts/{account}/omnichannelSettings/{omnichannel_setting}`
parent = f"accounts/{_ACCOUNT}/omnichannelSettings/{region_code}"
# Creates the request.
request = FindLfpProvidersRequest(parent=parent)
print("Sending find LFP providers request:")
# Makes the request and catches and prints any error messages.
try:
# Calls the API to find LFP providers.
response = client.find_lfp_providers(request=request)
count = 0
# Iterates over all the entries in the response and prints them.
for lfp_provider in response:
print(lfp_provider)
count += 1
print(f"The following count of elements were returned: {count}")
except RuntimeError as e:
print("An error has occured: ")
print(e)
if __name__ == "__main__":
# The country you're targeting.
_REGION_CODE = "{REGION_CODE}"
find_lfp_providers(_REGION_CODE)