取得節目
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Merchant API 程式碼範例,用於取得計畫。
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.GetProgramRequest;
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 get a single shopping program resource for a Merchant Center
* account.
*/
public class GetProgramSample {
public static void getProgram(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}
GetProgramRequest request = GetProgramRequest.newBuilder().setName(name).build();
System.out.println("Sending Get Program request:");
Program response = programsServiceClient.getProgram(request);
System.out.println("Retrieved 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 retrieve.
String program = "free-listings";
getProgram(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\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();
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 getting a program resource for a Merchant Center account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import GetProgramRequest
from google.shopping.merchant_accounts_v1 import ProgramsServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def get_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)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
program = "free-listings"
get_program(program)
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-21 (世界標準時間)。
[null,null,["上次更新時間:2025-08-21 (世界標準時間)。"],[[["\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,["# Get a program\n\nMerchant API code sample to get a 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.GetProgramRequest;\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 /**\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/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/programs/v1/GetProgramSample.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\\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/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/programs/v1/GetProgramSample.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 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_v1 import GetProgramRequest\n from google.shopping.merchant_accounts_v1 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/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/programs/v1/get_program_sample.py"]]