Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to get a program.
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.packageshopping.merchant.samples.accounts.programs.v1;importcom.google.api.gax.core.FixedCredentialsProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.shopping.merchant.accounts.v1.GetProgramRequest;importcom.google.shopping.merchant.accounts.v1.Program;importcom.google.shopping.merchant.accounts.v1.ProgramName;importcom.google.shopping.merchant.accounts.v1.ProgramsServiceClient;importcom.google.shopping.merchant.accounts.v1.ProgramsServiceSettings;importshopping.merchant.samples.utils.Authenticator;importshopping.merchant.samples.utils.Config;/** * This class demonstrates how to get a single shopping program resource for a Merchant Center * account. */publicclassGetProgramSample{publicstaticvoidgetProgram(Configconfig,Stringprogram)throwsException{// Obtains OAuth token based on the user's configuration.GoogleCredentialscredential=newAuthenticator().authenticate();// Creates service settings using the credentials retrieved above.ProgramsServiceSettingsprogramsServiceSettings=ProgramsServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Creates program name to identify the program.Stringname=ProgramName.newBuilder().setAccount(config.getAccountId().toString()).setProgram(program).build().toString();// Calls the API and catches and prints any network failures/errors.try(ProgramsServiceClientprogramsServiceClient=ProgramsServiceClient.create(programsServiceSettings)){// The name has the format: accounts/{account}/programs/{program}GetProgramRequestrequest=GetProgramRequest.newBuilder().setName(name).build();System.out.println("Sending Get Program request:");Programresponse=programsServiceClient.getProgram(request);System.out.println("Retrieved Program below");System.out.println(response);}catch(Exceptione){System.out.println(e);}}publicstaticvoidmain(String[]args)throwsException{Configconfig=Config.load();// Replace this with the name of the program to retrieve.Stringprogram="free-listings";getProgram(config,program);}}
<?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\GetProgramRequest;/** * This class demonstrates how to get a single shopping program resource for a Merchant Center * account. */class GetProgramSample{ /** * Retrieves 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 retrieve. * @return void */ public static function getProgram($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 GetProgramRequest(['name' => $name]); print "Sending Get Program request:\n"; $response = $programsServiceClient->getProgram($request); print "Retrieved 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 retrieve. $program = "free-listings"; self::getProgram($config, $program); }}// Run the script$sample = new GetProgramSample();$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 for getting a program resource for a Merchant Center account."""fromexamples.authenticationimportconfigurationfromexamples.authenticationimportgenerate_user_credentialsfromgoogle.shopping.merchant_accounts_v1importGetProgramRequestfromgoogle.shopping.merchant_accounts_v1importProgramsServiceClient_ACCOUNT=configuration.Configuration().read_merchant_info()defget_program(program_):"""Gets a program resource for a 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=GetProgramRequest(name=name)# Makes the request and catches and prints any error messages.try:print("Sending Get Program request:")response=client.get_program(request=request)print("Retrieved Program below")print(response)exceptRuntimeErrorase:print(e)if__name__=="__main__":program="free-listings"get_program(program)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-13 UTC."],[[["\u003cp\u003eThis content provides code samples in Java and Python demonstrating how to retrieve a single shopping program resource for a Merchant Center account.\u003c/p\u003e\n"],["\u003cp\u003eThe Java code utilizes \u003ccode\u003eProgramsServiceClient\u003c/code\u003e and \u003ccode\u003eGetProgramRequest\u003c/code\u003e to communicate with the Merchant API, authenticating using OAuth credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe Python code also uses \u003ccode\u003eProgramsServiceClient\u003c/code\u003e and \u003ccode\u003eGetProgramRequest\u003c/code\u003e to retrieve program information and requires user OAuth credentials.\u003c/p\u003e\n"],["\u003cp\u003eBoth samples set up the request for getting the information for the program, sends it, and prints the response or any errors.\u003c/p\u003e\n"],["\u003cp\u003eThe main program call in both examples was designed to retrieve the program 'free-listings'.\u003c/p\u003e\n"]]],["The provided code demonstrates how to retrieve a specific shopping program resource for a Merchant Center account using the Merchant API in Java, PHP, and Python. It involves authenticating via OAuth, creating a `ProgramsServiceClient`, constructing a `GetProgramRequest` with the account and program name, sending the request, and receiving the program details. The code also handles potential API errors, printing error messages if the request fails. Each code provides the program name as \"free-listings\".\n"],null,["Merchant API code sample to get program \n\nJava \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.v1beta;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1beta.GetProgramRequest;\n import com.google.shopping.merchant.accounts.v1beta.Program;\n import com.google.shopping.merchant.accounts.v1beta.ProgramName;\n import com.google.shopping.merchant.accounts.v1beta.ProgramsServiceClient;\n import com.google.shopping.merchant.accounts.v1beta.ProgramsServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /**\n * This class demonstrates how to get a single shopping program resource for a Merchant Center\n * account.\n */\n public class GetProgramSample {\n\n public static void getProgram(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 GetProgramRequest request = GetProgramRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending Get Program request:\");\n Program response = programsServiceClient.getProgram(request);\n\n System.out.println(\"Retrieved 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 retrieve.\n String program = \"free-listings\";\n\n getProgram(config, program);\n }\n }\n\n https://github.com/google/merchant-api-samples/blob/9105060072cf14e232bf8e3d3d3964a659b10984/java/src/main/java/shopping/merchant/samples/accounts/programs/v1beta/GetProgramSample.java\n\nPHP \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\\V1beta\\Client\\ProgramsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1beta\\GetProgramRequest;\n\n /**\n * This class demonstrates how to get a single shopping program resource for a Merchant Center\n * account.\n */\n class GetProgramSample\n {\n\n /**\n * Retrieves 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 retrieve.\n * @return void\n */\n public static function getProgram($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 // 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 GetProgramRequest(['name' =\u003e $name]);\n\n print \"Sending Get Program request:\\n\";\n $response = $programsServiceClient-\u003egetProgram($request);\n\n print \"Retrieved Program below\\n\";\n print_r($response);\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\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 retrieve.\n $program = \"free-listings\";\n self::getProgram($config, $program);\n }\n }\n\n // Run the script\n $sample = new GetProgramSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/9105060072cf14e232bf8e3d3d3964a659b10984/php/examples/accounts/programs/v1beta/GetProgramSample.php\n\nPython \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 for getting a program resource 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_v1beta import GetProgramRequest\n from google.shopping.merchant_accounts_v1beta import ProgramsServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def get_program(program_):\n \"\"\"Gets a program resource for a 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 = GetProgramRequest(name=name)\n\n # Makes the request and catches and prints any error messages.\n try:\n print(\"Sending Get Program request:\")\n response = client.get_program(request=request)\n print(\"Retrieved Program below\")\n print(response)\n except RuntimeError as e:\n print(e)\n\n if __name__ == \"__main__\":\n program = \"free-listings\"\n get_program(program)\n\n https://github.com/google/merchant-api-samples/blob/9105060072cf14e232bf8e3d3d3964a659b10984/python/examples/accounts/programs/v1beta/get_program_sample.py"]]