用于更新首页的 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.homepages.v1beta;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.accounts.v1beta.Homepage;
import com.google.shopping.merchant.accounts.v1beta.HomepageName;
import com.google.shopping.merchant.accounts.v1beta.HomepageServiceClient;
import com.google.shopping.merchant.accounts.v1beta.HomepageServiceSettings;
import com.google.shopping.merchant.accounts.v1beta.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
/**
* 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\V1beta\Homepage;
use Google\Shopping\Merchant\Accounts\V1beta\Client\HomepageServiceClient;
use Google\Shopping\Merchant\Accounts\V1beta\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();
# -*- 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_v1beta import Homepage
from google.shopping.merchant_accounts_v1beta import HomepageServiceClient
from google.shopping.merchant_accounts_v1beta 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)