इलाकों की सूची बनाने के लिए Merchant API का कोड सैंपल
// 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.regions.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.ListRegionsRequest;
import com.google.shopping.merchant.accounts.v1beta.Region;
import com.google.shopping.merchant.accounts.v1beta.RegionsServiceClient;
import com.google.shopping.merchant.accounts.v1beta.RegionsServiceClient.ListRegionsPagedResponse;
import com.google.shopping.merchant.accounts.v1beta.RegionsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to list all the regions for a given Merchant Center account. */
public class ListRegionsSample {
private static String getParent(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void listRegions(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.
RegionsServiceSettings regionsServiceSettings =
RegionsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify the account from which to list all regions.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (RegionsServiceClient regionsServiceClient =
RegionsServiceClient.create(regionsServiceSettings)) {
// The parent has the format: accounts/{account}
ListRegionsRequest request = ListRegionsRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list regions request:");
ListRegionsPagedResponse response = regionsServiceClient.listRegions(request);
int count = 0;
// Iterates over all rows in all pages and prints the region
// in each row.
// `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
// request to fetch all pages of data.
for (Region element : response.iterateAll()) {
System.out.println(element);
count++;
}
System.out.print("The following count of elements were returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listRegions(config);
}
}
<?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\V1beta\Client\RegionsServiceClient;
use Google\Shopping\Merchant\Accounts\V1beta\ListRegionsRequest;
/**
* This class demonstrates how to list all the regions for a given Merchant Center account.
*/
class ListRegionsSample
{
private static function getParent(string $accountId): string
{
return sprintf("accounts/%s", $accountId);
}
public static function listRegionsSample(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.
$regionsServiceClient = new RegionsServiceClient($options);
// Creates parent to identify the account from which to list all regions.
$parent = self::getParent($config['accountId']);
try {
$request = new ListRegionsRequest([
'parent' => $parent
]);
print "Sending list regions request:\n";
$response = $regionsServiceClient->listRegions($request);
$count = 0;
foreach ($response->iterateAllElements() as $element) {
print $element->getName() . PHP_EOL;
$count++;
}
print "The following count of elements were returned: ";
print $count . PHP_EOL;
} catch (ApiException $e) {
print $e->getMessage();
}
}
public function callSample(): void
{
$config = Config::generateConfig();
self::listRegionsSample($config);
}
}
$sample = new ListRegionsSample();
$sample->callSample();
# -*- 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 list all the Regions for a given Merchant Center account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1beta import ListRegionsRequest
from google.shopping.merchant_accounts_v1beta import RegionsServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def get_parent(account_id):
return f"accounts/{account_id}"
def list_regions():
"""Lists all the regions for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = RegionsServiceClient(credentials=credentials)
# Creates parent to identify the account from which to list all regions.
parent = get_parent(_ACCOUNT)
# Creates the request.
request = ListRegionsRequest(parent=parent)
# Makes the request and catches and prints any error messages.
try:
response = client.list_regions(request=request)
count = 0
print("Sending list regions request:")
for element in response:
print(element)
count += 1
print(f"The following count of elements were returned: {count}")
except RuntimeError as e:
print("List region request failed!")
print(e)
if __name__ == "__main__":
list_regions()