获取自动 Feed 设置
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用于获取自动 Feed 设置的 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.autofeedsettings.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.AutofeedSettings;
import com.google.shopping.merchant.accounts.v1.AutofeedSettingsName;
import com.google.shopping.merchant.accounts.v1.AutofeedSettingsServiceClient;
import com.google.shopping.merchant.accounts.v1.AutofeedSettingsServiceSettings;
import com.google.shopping.merchant.accounts.v1.GetAutofeedSettingsRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to get the autofeed settings of a Merchant Center account. */
public class GetAutofeedSettingsSample {
public static void getAutofeedSettings(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.
AutofeedSettingsServiceSettings autofeedSettingsServiceSettings =
AutofeedSettingsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates AutofeedSettings name to identify the AutofeedSettings.
String name =
AutofeedSettingsName.newBuilder()
.setAccount(config.getAccountId().toString())
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (AutofeedSettingsServiceClient autofeedSettingsServiceClient =
AutofeedSettingsServiceClient.create(autofeedSettingsServiceSettings)) {
// The name has the format: accounts/{account}/autofeedSettings
GetAutofeedSettingsRequest request =
GetAutofeedSettingsRequest.newBuilder().setName(name).build();
System.out.println("Sending get AutofeedSettings request:");
AutofeedSettings response = autofeedSettingsServiceClient.getAutofeedSettings(request);
System.out.println("Retrieved AutofeedSettings below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
getAutofeedSettings(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\AutofeedSettingsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\GetAutofeedSettingsRequest;
/**
* This class demonstrates how to get the autofeed settings of a Merchant Center account.
*/
class GetAutofeedSettingsSample
{
/**
* Get the autofeed settings of a Merchant Center account.
*
* @param array $config The configuration data for authentication and account ID.
* @return void
*/
public static function getAutofeedSettingsSample(array $config): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Create options for the client.
$options = ['credentials' => $credentials];
// Create a client.
$autofeedSettingsServiceClient = new AutofeedSettingsServiceClient($options);
// Create the AutofeedSettings name.
$name = "accounts/" . $config['accountId'] . "/autofeedSettings";
// Call the API.
try {
// Prepare the request.
$request = (new GetAutofeedSettingsRequest())
->setName($name);
print "Sending get AutofeedSettings request:\n";
$response = $autofeedSettingsServiceClient->getAutofeedSettings($request);
print "Retrieved AutofeedSettings 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::getAutofeedSettingsSample($config);
}
}
// Run the script
$sample = new GetAutofeedSettingsSample();
$sample->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.
"""A module to get AutofeedSettings."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import AutofeedSettingsServiceClient
from google.shopping.merchant_accounts_v1 import GetAutofeedSettingsRequest
_ACCOUNT = configuration.Configuration().read_merchant_info()
def get_autofeed_settings():
"""Gets the AutofeedSettings of a Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = AutofeedSettingsServiceClient(credentials=credentials)
# Creates name to identify the AutofeedSettings.
name = "accounts/" + _ACCOUNT + "/autofeedSettings"
# Creates the request.
request = GetAutofeedSettingsRequest(name=name)
# Makes the request and catches and prints any error messages.
try:
response = client.get_autofeed_settings(request=request)
print("Retrieved AutofeedSettings below")
print(response)
except RuntimeError as e:
print("Get AutofeedSettings request failed")
print(e)
if __name__ == "__main__":
get_autofeed_settings()
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis code sample demonstrates how to retrieve the autofeed settings for a specific Merchant Center account using the Merchant API in Java.\u003c/p\u003e\n"],["\u003cp\u003eIt utilizes the \u003ccode\u003eAutofeedSettingsServiceClient\u003c/code\u003e to send a \u003ccode\u003eGetAutofeedSettingsRequest\u003c/code\u003e to the API, specifying the account's autofeed settings name.\u003c/p\u003e\n"],["\u003cp\u003eThe sample uses Google OAuth 2.0 credentials for authentication and constructs the service client with these credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe response from the API contains the autofeed settings associated with the given account, which are then printed to the console.\u003c/p\u003e\n"]]],[],null,["# Get autofeed settings\n\nMerchant API code sample to get autofeed settings. \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.autofeedsettings.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.AutofeedSettings;\n import com.google.shopping.merchant.accounts.v1.AutofeedSettingsName;\n import com.google.shopping.merchant.accounts.v1.AutofeedSettingsServiceClient;\n import com.google.shopping.merchant.accounts.v1.AutofeedSettingsServiceSettings;\n import com.google.shopping.merchant.accounts.v1.GetAutofeedSettingsRequest;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to get the autofeed settings of a Merchant Center account. */\n public class GetAutofeedSettingsSample {\n\n public static void getAutofeedSettings(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 AutofeedSettingsServiceSettings autofeedSettingsServiceSettings =\n AutofeedSettingsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates AutofeedSettings name to identify the AutofeedSettings.\n String name =\n AutofeedSettingsName.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 (AutofeedSettingsServiceClient autofeedSettingsServiceClient =\n AutofeedSettingsServiceClient.create(autofeedSettingsServiceSettings)) {\n\n // The name has the format: accounts/{account}/autofeedSettings\n GetAutofeedSettingsRequest request =\n GetAutofeedSettingsRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending get AutofeedSettings request:\");\n AutofeedSettings response = autofeedSettingsServiceClient.getAutofeedSettings(request);\n\n System.out.println(\"Retrieved AutofeedSettings 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 getAutofeedSettings(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/autofeedsettings/v1/GetAutofeedSettingsSample.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\\AutofeedSettingsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\GetAutofeedSettingsRequest;\n\n /**\n * This class demonstrates how to get the autofeed settings of a Merchant Center account.\n */\n class GetAutofeedSettingsSample\n {\n /**\n * Get the autofeed settings of a Merchant Center account.\n *\n * @param array $config The configuration data for authentication and account ID.\n * @return void\n */\n public static function getAutofeedSettingsSample(array $config): void\n {\n // Get OAuth credentials.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Create options for the client.\n $options = ['credentials' =\u003e $credentials];\n\n // Create a client.\n $autofeedSettingsServiceClient = new AutofeedSettingsServiceClient($options);\n\n // Create the AutofeedSettings name.\n $name = \"accounts/\" . $config['accountId'] . \"/autofeedSettings\";\n\n // Call the API.\n try {\n // Prepare the request.\n $request = (new GetAutofeedSettingsRequest())\n -\u003esetName($name);\n\n print \"Sending get AutofeedSettings request:\\n\";\n $response = $autofeedSettingsServiceClient-\u003egetAutofeedSettings($request);\n\n print \"Retrieved AutofeedSettings 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::getAutofeedSettingsSample($config);\n }\n }\n\n\n // Run the script\n $sample = new GetAutofeedSettingsSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/autofeedsettings/v1/GetAutofeedSettingsSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\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 # 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 AutofeedSettings.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import AutofeedSettingsServiceClient\n from google.shopping.merchant_accounts_v1 import GetAutofeedSettingsRequest\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def get_autofeed_settings():\n \"\"\"Gets the AutofeedSettings of a Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = AutofeedSettingsServiceClient(credentials=credentials)\n\n # Creates name to identify the AutofeedSettings.\n name = \"accounts/\" + _ACCOUNT + \"/autofeedSettings\"\n\n # Creates the request.\n request = GetAutofeedSettingsRequest(name=name)\n\n # Makes the request and catches and prints any error messages.\n try:\n response = client.get_autofeed_settings(request=request)\n print(\"Retrieved AutofeedSettings below\")\n print(response)\n except RuntimeError as e:\n print(\"Get AutofeedSettings request failed\")\n print(e)\n\n\n if __name__ == \"__main__\":\n get_autofeed_settings() \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/autofeedsettings/v1/get_autofeed_settings_sample.py"]]