Atualizar uma página inicial
Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Exemplo de código da API Merchant para atualizar uma página inicial.
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.homepages.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.Homepage;
import com.google.shopping.merchant.accounts.v1.HomepageName;
import com.google.shopping.merchant.accounts.v1.HomepageServiceClient;
import com.google.shopping.merchant.accounts.v1.HomepageServiceSettings;
import com.google.shopping.merchant.accounts.v1.UpdateHomepageRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to update a homepage to a new URL. */
public class UpdateHomepageSample {
public static void updateHomepage(Config config, String uri) throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
HomepageServiceSettings homepageServiceSettings =
HomepageServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates homepage name to identify homepage.
String name =
HomepageName.newBuilder().setAccount(config.getAccountId().toString()).build().toString();
// Create a homepage with the updated fields.
Homepage homepage = Homepage.newBuilder().setName(name).setUri(uri).build();
FieldMask fieldMask = FieldMask.newBuilder().addPaths("uri").build();
try (HomepageServiceClient homepageServiceClient =
HomepageServiceClient.create(homepageServiceSettings)) {
UpdateHomepageRequest request =
UpdateHomepageRequest.newBuilder().setHomepage(homepage).setUpdateMask(fieldMask).build();
System.out.println("Sending Update Homepage request");
Homepage response = homepageServiceClient.updateHomepage(request);
System.out.println("Updated Homepage 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();
// The URI (a URL) of the store's homepage.
String uri = "https://example.com";
updateHomepage(config, uri);
}
}
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\Homepage;
use Google\Shopping\Merchant\Accounts\V1\Client\HomepageServiceClient;
use Google\Shopping\Merchant\Accounts\V1\UpdateHomepageRequest;
/**
* This class demonstrates how to update a homepage to a new URL.
*/
class UpdateHomepage
{
/**
* Updates a homepage to a new URL.
*
* @param array $config The configuration data for authentication and account ID.
* @param string $uri The new URI for the homepage.
* @return void
* @throws ApiException if the API call fails.
*/
public static function updateHomepageSample(array $config, string $uri): 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.
$homepageServiceClient = new HomepageServiceClient($options);
// Creates Homepage name to identify Homepage.
// The name has the format: accounts/{account}/homepage
$name = "accounts/" . $config['accountId'] . "/homepage";
// Create a homepage with the updated fields.
$homepage = new Homepage(['name' => $name, 'uri' => $uri]);
// Create field mask to specify which fields to update.
$fieldMask = new FieldMask(['paths' => ['uri']]);
try {
$request = new UpdateHomepageRequest([
'homepage' => $homepage,
'update_mask' => $fieldMask
]);
print "Sending Update Homepage request\n";
$response = $homepageServiceClient->updateHomepage($request);
print "Updated Homepage 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();
// The URI (a URL) of the store's homepage.
$uri = "https://example.com";
// Makes the call to update the homepage.
self::updateHomepageSample($config, $uri);
}
}
// Run the script
$sample = new UpdateHomepage();
$sample->callSample();
Python
# -*- 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 update a Homepage."""
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 Homepage
from google.shopping.merchant_accounts_v1 import HomepageServiceClient
from google.shopping.merchant_accounts_v1 import UpdateHomepageRequest
_ACCOUNT = configuration.Configuration().read_merchant_info()
def update_homepage(new_uri):
"""Updates a homepage to a new URL."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = HomepageServiceClient(credentials=credentials)
# Creates Homepage name to identify Homepage.
name = "accounts/" + _ACCOUNT + "/homepage"
# Create a homepage with the updated fields.
homepage = Homepage(name=name, uri=new_uri)
# Create a FieldMask for the "uri" field.
field_mask = field_mask_pb2.FieldMask(paths=["uri"])
# Creates the request.
request = UpdateHomepageRequest(homepage=homepage, update_mask=field_mask)
# Makes the request and catches and prints any error messages.
try:
response = client.update_homepage(request=request)
print("Updated Homepage Name below")
print(response.name)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
# The URI (a URL) of the store's homepage.
uri = "https://example.com"
update_homepage(uri)
Exceto em caso de indicação contrária, o conteúdo desta página é licenciado de acordo com a Licença de atribuição 4.0 do Creative Commons, e as amostras de código são licenciadas de acordo com a Licença Apache 2.0. Para mais detalhes, consulte as políticas do site do Google Developers. Java é uma marca registrada da Oracle e/ou afiliadas.
Última atualização 2025-08-21 UTC.
[null,null,["Última atualização 2025-08-21 UTC."],[[["\u003cp\u003eThis page provides code samples in Java and Python demonstrating how to update a merchant's homepage URL using the Google Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe Java code utilizes the \u003ccode\u003eHomepageServiceClient\u003c/code\u003e to send an \u003ccode\u003eUpdateHomepageRequest\u003c/code\u003e, modifying the homepage's URI and updating it using a \u003ccode\u003eFieldMask\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe Python code also employs a \u003ccode\u003eHomepageServiceClient\u003c/code\u003e to update the homepage URL, constructing an \u003ccode\u003eUpdateHomepageRequest\u003c/code\u003e with a \u003ccode\u003eFieldMask\u003c/code\u003e to specify the \u003ccode\u003euri\u003c/code\u003e field for modification.\u003c/p\u003e\n"],["\u003cp\u003eBoth samples require authentication credentials and include error handling to catch and display potential issues during the update process.\u003c/p\u003e\n"],["\u003cp\u003eBoth code examples provide a direct way to modify a merchant account's homepage URL to a different one.\u003c/p\u003e\n"]]],["The provided code demonstrates how to update a store's homepage URL using the Merchant API in Java, PHP, and Python. The core actions involve: authenticating with OAuth credentials, creating a `HomepageServiceClient`, defining the homepage name with an account ID, creating a `Homepage` object with the new URL, and specifying a `FieldMask` for updating only the URL. Finally, it constructs an `UpdateHomepageRequest` and sends it using the client to update the homepage. Each example then prints the updated homepage name.\n"],null,["# Update an homepage\n\nMerchant API code sample to update an homepage. \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.homepages.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.Homepage;\n import com.google.shopping.merchant.accounts.v1.HomepageName;\n import com.google.shopping.merchant.accounts.v1.HomepageServiceClient;\n import com.google.shopping.merchant.accounts.v1.HomepageServiceSettings;\n import com.google.shopping.merchant.accounts.v1.UpdateHomepageRequest;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to update a homepage to a new URL. */\n public class UpdateHomepageSample {\n\n public static void updateHomepage(Config config, String uri) throws Exception {\n\n GoogleCredentials credential = new Authenticator().authenticate();\n\n HomepageServiceSettings homepageServiceSettings =\n HomepageServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates homepage name to identify homepage.\n String name =\n HomepageName.newBuilder().setAccount(config.getAccountId().toString()).build().toString();\n\n // Create a homepage with the updated fields.\n Homepage homepage = Homepage.newBuilder().setName(name).setUri(uri).build();\n\n FieldMask fieldMask = FieldMask.newBuilder().addPaths(\"uri\").build();\n\n try (HomepageServiceClient homepageServiceClient =\n HomepageServiceClient.create(homepageServiceSettings)) {\n\n UpdateHomepageRequest request =\n UpdateHomepageRequest.newBuilder().setHomepage(homepage).setUpdateMask(fieldMask).build();\n\n System.out.println(\"Sending Update Homepage request\");\n Homepage response = homepageServiceClient.updateHomepage(request);\n System.out.println(\"Updated Homepage 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 // The URI (a URL) of the store's homepage.\n String uri = \"https://example.com\";\n\n updateHomepage(config, uri);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/homepages/v1/UpdateHomepageSample.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\\Homepage;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\HomepageServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\UpdateHomepageRequest;\n\n /**\n * This class demonstrates how to update a homepage to a new URL.\n */\n class UpdateHomepage\n {\n /**\n * Updates a homepage to a new URL.\n *\n * @param array $config The configuration data for authentication and account ID.\n * @param string $uri The new URI for the homepage.\n * @return void\n * @throws ApiException if the API call fails.\n */\n public static function updateHomepageSample(array $config, string $uri): void\n {\n // Gets the OAuth credentials to make the request.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Creates options config containing credentials for the client to use.\n $options = ['credentials' =\u003e $credentials];\n\n // Creates a client.\n $homepageServiceClient = new HomepageServiceClient($options);\n\n // Creates Homepage name to identify Homepage.\n // The name has the format: accounts/{account}/homepage\n $name = \"accounts/\" . $config['accountId'] . \"/homepage\";\n\n // Create a homepage with the updated fields.\n $homepage = new Homepage(['name' =\u003e $name, 'uri' =\u003e $uri]);\n\n // Create field mask to specify which fields to update.\n $fieldMask = new FieldMask(['paths' =\u003e ['uri']]);\n\n try {\n $request = new UpdateHomepageRequest([\n 'homepage' =\u003e $homepage,\n 'update_mask' =\u003e $fieldMask\n ]);\n\n print \"Sending Update Homepage request\\n\";\n $response = $homepageServiceClient-\u003eupdateHomepage($request);\n print \"Updated Homepage 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 // The URI (a URL) of the store's homepage.\n $uri = \"https://example.com\";\n\n // Makes the call to update the homepage.\n self::updateHomepageSample($config, $uri);\n }\n }\n\n // Run the script\n $sample = new UpdateHomepage();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/homepages/v1/UpdateHomepageSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\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 # 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 a Homepage.\"\"\"\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 Homepage\n from google.shopping.merchant_accounts_v1 import HomepageServiceClient\n from google.shopping.merchant_accounts_v1 import UpdateHomepageRequest\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def update_homepage(new_uri):\n \"\"\"Updates a homepage to a new URL.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = HomepageServiceClient(credentials=credentials)\n\n # Creates Homepage name to identify Homepage.\n name = \"accounts/\" + _ACCOUNT + \"/homepage\"\n\n # Create a homepage with the updated fields.\n homepage = Homepage(name=name, uri=new_uri)\n\n # Create a FieldMask for the \"uri\" field.\n field_mask = field_mask_pb2.FieldMask(paths=[\"uri\"])\n\n # Creates the request.\n request = UpdateHomepageRequest(homepage=homepage, update_mask=field_mask)\n\n # Makes the request and catches and prints any error messages.\n try:\n response = client.update_homepage(request=request)\n print(\"Updated Homepage Name below\")\n print(response.name)\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n # The URI (a URL) of the store's homepage.\n uri = \"https://example.com\"\n update_homepage(uri)\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/homepages/v1/update_homepage_sample.py"]]