获取配送设置

用于获取配送设置的商家 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.shippingsettings.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.GetShippingSettingsRequest;
import com.google.shopping.merchant.accounts.v1beta.ShippingSettings;
import com.google.shopping.merchant.accounts.v1beta.ShippingSettingsName;
import com.google.shopping.merchant.accounts.v1beta.ShippingSettingsServiceClient;
import com.google.shopping.merchant.accounts.v1beta.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
/**
 * 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\ShippingSettingsServiceClient;
use Google\Shopping\Merchant\Accounts\V1beta\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();
# -*- 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 the ShippingSettings 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 GetShippingSettingsRequest
from google.shopping.merchant_accounts_v1beta import ShippingSettingsServiceClient

_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"


def get_shipping_settings():
  """Gets the ShippingSettings for a given Merchant Center account."""

  # Gets OAuth Credentials.
  credentials = generate_user_credentials.main()

  # Creates a client.
  client = ShippingSettingsServiceClient(credentials=credentials)

  # Creates the Shipping Settings name
  name = _PARENT + "/shippingSettings"

  # Creates the request.
  request = GetShippingSettingsRequest(name=name)

  # Makes the request and prints the retrieved ShippingSettings.
  try:
    response = client.get_shipping_settings(request=request)
    print("Retrieved ShippingSettings below")
    print(response)
  except RuntimeError as e:
    print(e)


if __name__ == "__main__":
  get_shipping_settings()