Activer le programme
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Exemple de code de l'API Merchant pour activer le programme.
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.programs.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.EnableProgramRequest;
import com.google.shopping.merchant.accounts.v1.Program;
import com.google.shopping.merchant.accounts.v1.ProgramName;
import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient;
import com.google.shopping.merchant.accounts.v1.ProgramsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to enable a shopping program for a Merchant Center account. */
public class EnableProgramSample {
public static void enableProgram(Config config, String program) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
ProgramsServiceSettings programsServiceSettings =
ProgramsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates program name to identify the program.
String name =
ProgramName.newBuilder()
.setAccount(config.getAccountId().toString())
.setProgram(program)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (ProgramsServiceClient programsServiceClient =
ProgramsServiceClient.create(programsServiceSettings)) {
// The name has the format: accounts/{account}/programs/{program}
EnableProgramRequest request = EnableProgramRequest.newBuilder().setName(name).build();
System.out.println("Sending Enable Program request:");
Program response = programsServiceClient.enableProgram(request);
System.out.println("Enabled Program below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// Replace this with the name of the program to be enabled.
String program = "free-listings";
enableProgram(config, program);
}
}
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\ProgramsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\EnableProgramRequest;
/**
* This class demonstrates how to enable a shopping program for a Merchant Center account.
*/
class EnableProgramSample
{
/**
* Enables a program for the given Merchant Center account.
*
* @param array $config The configuration data for authentication and account ID.
* @param string $program The program to enable.
* @return void
*/
public static function enableProgram($config, $program): 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.
$programsServiceClient = new ProgramsServiceClient($options);
// Creates program name to identify the program.
$name = $parent = "accounts/" . $config['accountId'] . "/programs/" . $program;
// Calls the API and catches and prints any network failures/errors.
try {
// The name has the format: accounts/{account}/programs/{program}
$request = new EnableProgramRequest(['name' => $name]);
print "Sending Enable Program request:\n";
$response = $programsServiceClient->enableProgram($request);
print "Enabled Program 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();
// Replace this with the name of the program to be enabled.
$program = "free-listings";
self::enableProgram($config, $program);
}
}
// Run the script
$sample = new EnableProgramSample();
$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 for enabling a program for a Merchant Center account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import EnableProgramRequest
from google.shopping.merchant_accounts_v1 import ProgramsServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def enable_program(program):
"""Enables a program for the given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = ProgramsServiceClient(credentials=credentials)
# Creates program name to identify the program.
name = "accounts/" + _ACCOUNT + "/programs/" + program
# Creates the request.
request = EnableProgramRequest(name=name)
# Makes the request and catches and prints any error messages.
try:
response = client.enable_program(request=request)
print("Enabled Program below")
print(response)
return response
except RuntimeError as e:
print(e)
return None
if __name__ == "__main__":
# Replace this with the name of the program to be enabled.
program_to_enable = "free-listings"
enable_program(program_to_enable)
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/08/21 (UTC).
[null,null,["Dernière mise à jour le 2025/08/21 (UTC)."],[[["\u003cp\u003eThis code sample demonstrates how to enable a shopping program for a Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe program utilizes the \u003ccode\u003eProgramsServiceClient\u003c/code\u003e to send an \u003ccode\u003eEnableProgramRequest\u003c/code\u003e to the API.\u003c/p\u003e\n"],["\u003cp\u003eThe code configures authentication using OAuth and sets up the \u003ccode\u003eProgramsServiceSettings\u003c/code\u003e with the retrieved credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe program name is constructed using the account ID and program identifier, following the format \u003ccode\u003eaccounts/{account}/programs/{program}\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eenableProgram\u003c/code\u003e function takes the configuration and the program name, to send a request and receive a response that is then outputted.\u003c/p\u003e\n"]]],[],null,["# Enable program\n\nMerchant API code sample to enable program. \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.programs.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.EnableProgramRequest;\n import com.google.shopping.merchant.accounts.v1.Program;\n import com.google.shopping.merchant.accounts.v1.ProgramName;\n import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient;\n import com.google.shopping.merchant.accounts.v1.ProgramsServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to enable a shopping program for a Merchant Center account. */\n public class EnableProgramSample {\n\n public static void enableProgram(Config config, String program) 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 ProgramsServiceSettings programsServiceSettings =\n ProgramsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates program name to identify the program.\n String name =\n ProgramName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setProgram(program)\n .build()\n .toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (ProgramsServiceClient programsServiceClient =\n ProgramsServiceClient.create(programsServiceSettings)) {\n\n // The name has the format: accounts/{account}/programs/{program}\n EnableProgramRequest request = EnableProgramRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending Enable Program request:\");\n Program response = programsServiceClient.enableProgram(request);\n\n System.out.println(\"Enabled Program 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 // Replace this with the name of the program to be enabled.\n String program = \"free-listings\";\n\n enableProgram(config, program);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/programs/v1/EnableProgramSample.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\\ProgramsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\EnableProgramRequest;\n\n /**\n * This class demonstrates how to enable a shopping program for a Merchant Center account.\n */\n class EnableProgramSample\n {\n /**\n * Enables a program for the given Merchant Center account.\n *\n * @param array $config The configuration data for authentication and account ID.\n * @param string $program The program to enable.\n * @return void\n */\n public static function enableProgram($config, $program): 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 $programsServiceClient = new ProgramsServiceClient($options);\n\n // Creates program name to identify the program.\n $name = $parent = \"accounts/\" . $config['accountId'] . \"/programs/\" . $program;\n\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n // The name has the format: accounts/{account}/programs/{program}\n $request = new EnableProgramRequest(['name' =\u003e $name]);\n\n print \"Sending Enable Program request:\\n\";\n $response = $programsServiceClient-\u003eenableProgram($request);\n\n print \"Enabled Program 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\n // Replace this with the name of the program to be enabled.\n $program = \"free-listings\";\n self::enableProgram($config, $program);\n }\n }\n\n // Run the script\n $sample = new EnableProgramSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/programs/v1/EnableProgramSample.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 for enabling a program for a Merchant Center account.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import EnableProgramRequest\n from google.shopping.merchant_accounts_v1 import ProgramsServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def enable_program(program):\n \"\"\"Enables a program for the given Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = ProgramsServiceClient(credentials=credentials)\n\n # Creates program name to identify the program.\n name = \"accounts/\" + _ACCOUNT + \"/programs/\" + program\n\n # Creates the request.\n request = EnableProgramRequest(name=name)\n\n # Makes the request and catches and prints any error messages.\n try:\n response = client.enable_program(request=request)\n print(\"Enabled Program below\")\n print(response)\n return response\n except RuntimeError as e:\n print(e)\n return None\n\n\n if __name__ == \"__main__\":\n # Replace this with the name of the program to be enabled.\n program_to_enable = \"free-listings\"\n enable_program(program_to_enable)\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/programs/v1/enable_program_sample.py"]]