Aggiornare le impostazioni del feed automatico
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Esempio di codice dell'API Merchant per aggiornare le impostazioni di autofeed.
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.protobuf.FieldMask;
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.UpdateAutofeedSettingsRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to update AutofeedSettings to be enabled. */
public class UpdateAutofeedSettingsSample {
public static void updateAutofeedSettings(Config config) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
AutofeedSettingsServiceSettings autofeedSettingsServiceSettings =
AutofeedSettingsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates AutofeedSettings name to identify AutofeedSettings.
String name =
AutofeedSettingsName.newBuilder()
.setAccount(config.getAccountId().toString())
.build()
.toString();
// Create AutofeedSettings with the updated fields.
AutofeedSettings autofeedSettings = AutofeedSettings.newBuilder().setName(name).build();
FieldMask fieldMask = FieldMask.newBuilder().addPaths("*").build();
try (AutofeedSettingsServiceClient autofeedSettingsServiceClient =
AutofeedSettingsServiceClient.create(autofeedSettingsServiceSettings)) {
UpdateAutofeedSettingsRequest request =
UpdateAutofeedSettingsRequest.newBuilder()
.setAutofeedSettings(autofeedSettings)
.setUpdateMask(fieldMask)
.build();
System.out.println("Sending Update AutofeedSettings request");
AutofeedSettings response = autofeedSettingsServiceClient.updateAutofeedSettings(request);
System.out.println("Updated AutofeedSettings Name below");
System.out.println(response.getName());
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
updateAutofeedSettings(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\Protobuf\FieldMask;
use Google\Shopping\Merchant\Accounts\V1\AutofeedSettings;
use Google\Shopping\Merchant\Accounts\V1\Client\AutofeedSettingsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\UpdateAutofeedSettingsRequest;
/**
* This class demonstrates how to update AutofeedSettings to be enabled.
*/
class UpdateAutofeedSettingsSample
{
/**
* Update AutofeedSettings to be enabled.
*
* @param array $config The configuration data for authentication and account ID.
* @return void
*/
public static function updateAutofeedSettingsSample(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";
// Create AutofeedSettings object.
$autofeedSettings = (new AutofeedSettings())
->setName($name);
// Create FieldMask.
$fieldMask = (new FieldMask())
->setPaths(["*"]);
// Call the API.
try {
// Prepare the request.
$request = (new UpdateAutofeedSettingsRequest())
->setAutofeedSettings($autofeedSettings)
->setUpdateMask($fieldMask);
print "Sending Update AutofeedSettings request\n";
$response = $autofeedSettingsServiceClient->updateAutofeedSettings($request);
print "Updated AutofeedSettings Name below\n";
print $response->getName() . "\n";
} catch (ApiException $e) {
print $e->getMessage();
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::updateAutofeedSettingsSample($config);
}
}
// Run the script
$sample = new UpdateAutofeedSettingsSample();
$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 update AutofeedSettings."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.protobuf import field_mask_pb2
from google.shopping.merchant_accounts_v1 import AutofeedSettings
from google.shopping.merchant_accounts_v1 import AutofeedSettingsServiceClient
from google.shopping.merchant_accounts_v1 import UpdateAutofeedSettingsRequest
_ACCOUNT = configuration.Configuration().read_merchant_info()
def update_autofeed_settings():
"""Updates 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"
# Create AutofeedSettings with the updated fields.
autofeed_settings = AutofeedSettings(name=name, enable_products=False)
# Create the field mask.
field_mask = field_mask_pb2.FieldMask(paths=["enable_products"])
# Creates the request.
request = UpdateAutofeedSettingsRequest(
autofeed_settings=autofeed_settings, update_mask=field_mask
)
# Makes the request and catches and prints any error messages.
try:
response = client.update_autofeed_settings(request=request)
print("Updated AutofeedSettings Name below")
print(response.name)
except RuntimeError as e:
print("Update AutofeedSettings request failed")
print(e)
if __name__ == "__main__":
update_autofeed_settings()
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
Ultimo aggiornamento 2025-08-21 UTC.
[null,null,["Ultimo aggiornamento 2025-08-21 UTC."],[[["\u003cp\u003eThis code sample demonstrates how to update AutofeedSettings using the Merchant API in Java.\u003c/p\u003e\n"],["\u003cp\u003eIt uses the \u003ccode\u003eAutofeedSettingsServiceClient\u003c/code\u003e to interact with the Merchant API and make the update request.\u003c/p\u003e\n"],["\u003cp\u003eThe code creates an \u003ccode\u003eAutofeedSettings\u003c/code\u003e object with updated fields, specifically focusing on enabling it.\u003c/p\u003e\n"],["\u003cp\u003eThe code builds and sends an \u003ccode\u003eUpdateAutofeedSettingsRequest\u003c/code\u003e, utilizing a \u003ccode\u003eFieldMask\u003c/code\u003e to indicate all fields should be updated, then prints the name of the updated settings.\u003c/p\u003e\n"]]],[],null,["# Update autofeed settings\n\nMerchant API code sample to update 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.protobuf.FieldMask;\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.UpdateAutofeedSettingsRequest;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to update AutofeedSettings to be enabled. */\n public class UpdateAutofeedSettingsSample {\n\n public static void updateAutofeedSettings(Config config) throws Exception {\n\n GoogleCredentials credential = new Authenticator().authenticate();\n\n AutofeedSettingsServiceSettings autofeedSettingsServiceSettings =\n AutofeedSettingsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates AutofeedSettings name to identify AutofeedSettings.\n String name =\n AutofeedSettingsName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .build()\n .toString();\n\n // Create AutofeedSettings with the updated fields.\n AutofeedSettings autofeedSettings = AutofeedSettings.newBuilder().setName(name).build();\n\n FieldMask fieldMask = FieldMask.newBuilder().addPaths(\"*\").build();\n\n try (AutofeedSettingsServiceClient autofeedSettingsServiceClient =\n AutofeedSettingsServiceClient.create(autofeedSettingsServiceSettings)) {\n\n UpdateAutofeedSettingsRequest request =\n UpdateAutofeedSettingsRequest.newBuilder()\n .setAutofeedSettings(autofeedSettings)\n .setUpdateMask(fieldMask)\n .build();\n\n System.out.println(\"Sending Update AutofeedSettings request\");\n AutofeedSettings response = autofeedSettingsServiceClient.updateAutofeedSettings(request);\n System.out.println(\"Updated AutofeedSettings Name below\");\n System.out.println(response.getName());\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 updateAutofeedSettings(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/autofeedsettings/v1/UpdateAutofeedSettingsSample.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\\Protobuf\\FieldMask;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\AutofeedSettings;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\AutofeedSettingsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\UpdateAutofeedSettingsRequest;\n\n /**\n * This class demonstrates how to update AutofeedSettings to be enabled.\n */\n class UpdateAutofeedSettingsSample\n {\n\n /**\n * Update AutofeedSettings to be enabled.\n *\n * @param array $config The configuration data for authentication and account ID.\n * @return void\n */\n public static function updateAutofeedSettingsSample(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 // Create AutofeedSettings object.\n $autofeedSettings = (new AutofeedSettings())\n -\u003esetName($name);\n\n // Create FieldMask.\n $fieldMask = (new FieldMask())\n -\u003esetPaths([\"*\"]);\n\n // Call the API.\n try {\n // Prepare the request.\n $request = (new UpdateAutofeedSettingsRequest())\n -\u003esetAutofeedSettings($autofeedSettings)\n -\u003esetUpdateMask($fieldMask);\n\n print \"Sending Update AutofeedSettings request\\n\";\n $response = $autofeedSettingsServiceClient-\u003eupdateAutofeedSettings($request);\n print \"Updated AutofeedSettings Name below\\n\";\n print $response-\u003egetName() . \"\\n\";\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\n self::updateAutofeedSettingsSample($config);\n }\n }\n\n // Run the script\n $sample = new UpdateAutofeedSettingsSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/autofeedsettings/v1/UpdateAutofeedSettingsSample.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 update AutofeedSettings.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.protobuf import field_mask_pb2\n from google.shopping.merchant_accounts_v1 import AutofeedSettings\n from google.shopping.merchant_accounts_v1 import AutofeedSettingsServiceClient\n from google.shopping.merchant_accounts_v1 import UpdateAutofeedSettingsRequest\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def update_autofeed_settings():\n \"\"\"Updates 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 # Create AutofeedSettings with the updated fields.\n autofeed_settings = AutofeedSettings(name=name, enable_products=False)\n\n # Create the field mask.\n field_mask = field_mask_pb2.FieldMask(paths=[\"enable_products\"])\n\n # Creates the request.\n request = UpdateAutofeedSettingsRequest(\n autofeed_settings=autofeed_settings, update_mask=field_mask\n )\n\n # Makes the request and catches and prints any error messages.\n try:\n response = client.update_autofeed_settings(request=request)\n print(\"Updated AutofeedSettings Name below\")\n print(response.name)\n except RuntimeError as e:\n print(\"Update AutofeedSettings request failed\")\n print(e)\n\n\n if __name__ == \"__main__\":\n update_autofeed_settings() \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/autofeedsettings/v1/update_autofeed_settings_sample.py"]]