List programs

Merchant API code sample to list programs.

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.AccountName 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.ListProgramsRequest 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.Program 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.ProgramsServiceClient 
 ; 
 import 
  
 com.google.shopping.merchant.accounts.v1.ProgramsServiceClient.ListProgramsPagedResponse 
 ; 
 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 list all shopping program resources for a Merchant Center account. 
 */ 
 public 
  
 class 
 ListProgramsSample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 listPrograms 
 ( 
 Config 
  
 config 
 ) 
  
 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 parent to identify the account for which to list programs. 
  
 String 
  
 parent 
  
 = 
  
 AccountName 
 . 
 of 
 ( 
 config 
 . 
 getAccountId 
 (). 
 toString 
 ()). 
 toString 
 (); 
  
 // Calls the API and catches and prints any network failures/errors. 
  
 try 
  
 ( 
 ProgramsServiceClient 
  
 programsServiceClient 
  
 = 
  
 ProgramsServiceClient 
 . 
 create 
 ( 
 programsServiceSettings 
 )) 
  
 { 
  
 ListProgramsRequest 
  
 request 
  
 = 
  
 ListProgramsRequest 
 . 
 newBuilder 
 (). 
 setParent 
 ( 
 parent 
 ). 
 build 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Sending List Programs request:" 
 ); 
  
 ListProgramsPagedResponse 
  
 response 
  
 = 
  
 programsServiceClient 
 . 
 listPrograms 
 ( 
 request 
 ); 
  
 int 
  
 count 
  
 = 
  
 0 
 ; 
  
 // Iterates over all programs in all pages and prints each program. 
  
 // Automatically uses the `nextPageToken`, if returned, to fetch all pages. 
  
 for 
  
 ( 
 Program 
  
 program 
  
 : 
  
 response 
 . 
 iterateAll 
 ()) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 program 
 ); 
  
 count 
 ++ 
 ; 
  
 } 
  
 System 
 . 
 out 
 . 
 print 
 ( 
 "The count of Programs returned: " 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 count 
 ); 
  
 } 
  
 catch 
  
 ( 
 Exception 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 e 
 ); 
  
 } 
  
 } 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 Config 
  
 config 
  
 = 
  
 Config 
 . 
 load 
 (); 
  
 listPrograms 
 ( 
 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\Shopping\Merchant\Accounts\V1\Client\ProgramsServiceClient; 
 use Google\Shopping\Merchant\Accounts\V1\ListProgramsRequest; 
 /** 
 * This class demonstrates how to list all shopping program resources for a Merchant Center account. 
 */ 
 class ListProgramsSample 
 { 
 /** 
 * Lists all programs for the given Merchant Center account. 
 * 
 * @param array $config The configuration data for authentication and account ID. 
 * @return void 
 */ 
 public static function listPrograms($config): 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 parent to identify the account for which to list programs. 
 $parent = "accounts/" . $config['accountId']; 
 // Calls the API and catches and prints any network failures/errors. 
 try { 
 $request = new ListProgramsRequest(['parent' => $parent]); 
 print "Sending List Programs request:\n"; 
 $response = $programsServiceClient->listPrograms($request); 
 $count = 0; 
 // Iterates over all programs in all pages and prints each program. 
 // Automatically uses the `nextPageToken`, if returned, to fetch all pages. 
 foreach ($response->iterateAllElements() as $program) { 
 print_r($program); 
 $count++; 
 } 
 print "The count of Programs returned: "; 
 print $count . "\n"; 
 } catch (ApiException $e) { 
 print $e->getMessage(); 
 } 
 } 
 /** 
 * Helper to execute the sample. 
 * 
 * @return void 
 */ 
 public function callSample(): void 
 { 
 $config = Config::generateConfig(); 
 self::listPrograms($config); 
 } 
 } 
 // Run the script 
 $sample = new ListProgramsSample(); 
 $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 for listing all program resources for a Merchant Center account.""" 
 from 
  
 examples.authentication 
  
 import 
 configuration 
 from 
  
 examples.authentication 
  
 import 
 generate_user_credentials 
 from 
  
 google.shopping.merchant_accounts_v1 
  
 import 
 ListProgramsRequest 
 from 
  
 google.shopping.merchant_accounts_v1 
  
 import 
 ProgramsServiceClient 
 _ACCOUNT 
 = 
 configuration 
 . 
 Configuration 
 () 
 . 
 read_merchant_info 
 () 
 def 
  
 list_programs 
 (): 
  
 """Lists all program resources for a Merchant Center account.""" 
 # Gets OAuth Credentials. 
 credentials 
 = 
 generate_user_credentials 
 . 
 main 
 () 
 # Creates a client. 
 client 
 = 
 ProgramsServiceClient 
 ( 
 credentials 
 = 
 credentials 
 ) 
 # Creates parent to identify the account for which to list programs. 
 parent 
 = 
 "accounts/" 
 + 
 _ACCOUNT 
 # Creates the request. 
 request 
 = 
 ListProgramsRequest 
 ( 
 parent 
 = 
 parent 
 ) 
 # Makes the request and catches and prints any error messages. 
 try 
 : 
 print 
 ( 
 "Sending List Programs request:" 
 ) 
 response 
 = 
 client 
 . 
 list_programs 
 ( 
 request 
 = 
 request 
 ) 
 count 
 = 
 0 
 # Iterates over all programs in all pages and prints each program. 
 for 
 program 
 in 
 response 
 : 
 print 
 ( 
 program 
 ) 
 count 
 += 
 1 
 print 
 ( 
 "The count of Programs returned: " 
 ) 
 print 
 ( 
 count 
 ) 
 except 
 RuntimeError 
 as 
 e 
 : 
 print 
 ( 
 e 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 list_programs 
 () 
  
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: