获取用户
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用于获取用户的 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.users.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.GetUserRequest;
import com.google.shopping.merchant.accounts.v1.User;
import com.google.shopping.merchant.accounts.v1.UserName;
import com.google.shopping.merchant.accounts.v1.UserServiceClient;
import com.google.shopping.merchant.accounts.v1.UserServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to get a single user for a given Merchant Center account. */
public class GetUserSample {
public static void getUser(Config config, String email) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates user name to identify user.
String name =
UserName.newBuilder()
.setAccount(config.getAccountId().toString())
.setEmail(email)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
// The name has the format: accounts/{account}/users/{email}
GetUserRequest request = GetUserRequest.newBuilder().setName(name).build();
System.out.println("Sending Get user request:");
User response = userServiceClient.getUser(request);
System.out.println("Retrieved User below");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The email address of this user. If you want to get the user information
// Of the user making the Content API request, you can also use "me" instead
// Of an email address.
String email = "testUser@gmail.com";
getUser(config, email);
}
}
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.
*/
/**
* Demonstrates how to get a single user for a given Merchant Center account.
*/
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\GetUserRequest;
use Google\Shopping\Merchant\Accounts\V1\Client\UserServiceClient;
/**
* Retrieves a user.
*
* @param array $config The configuration data.
* @param string $email The email address of the user.
* @return void
*/
function getUser($config, $email): 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.
$userServiceClient = new UserServiceClient($options);
// Creates user name to identify user.
$name = 'accounts/' . $config['accountId'] . "/users/" . $email;
// Calls the API and catches and prints any network failures/errors.
try {
$request = new GetUserRequest(['name' => $name]);
print "Sending Get user request:\n";
$response = $userServiceClient->getUser($request);
print "Retrieved User below\n";
print_r($response);
} catch (ApiException $e) {
print $e->getMessage();
}
}
$config = Config::generateConfig();
$email = "testUser@gmail.com";
getUser($config, $email);
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 to get a user."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import GetUserRequest
from google.shopping.merchant_accounts_v1 import UserServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def get_user(user_email):
"""Gets a single user for a given Merchant Center account."""
# Get OAuth credentials
credentials = generate_user_credentials.main()
# Create a UserServiceClient
client = UserServiceClient(credentials=credentials)
# Create user name string
name = "accounts/" + _ACCOUNT + "/users/" + user_email
# Create the request
request = GetUserRequest(name=name)
try:
print("Sending Get user request:")
response = client.get_user(request=request)
print("Retrieved User below")
print(response)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
# Modify this email to get the user details
email = "USER_MAIL_ACCOUNT"
get_user(email)
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-21。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eThis webpage provides code samples in Java, PHP, and Python for retrieving a single user from a given Merchant Center account.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample demonstrates the process of creating a \u003ccode\u003eGetUserRequest\u003c/code\u003e with the user's name, which follows the format \u003ccode\u003eaccounts/{account}/users/{email}\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe samples showcase the usage of the \u003ccode\u003eUserServiceClient\u003c/code\u003e to interact with the Merchant Center API and execute the \u003ccode\u003egetUser\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eThe Java and Python samples use OAuth for authentication, while the PHP code uses a service account or token file, for authenticating API requests.\u003c/p\u003e\n"],["\u003cp\u003eAll samples include error handling to manage potential network failures or API exceptions, and the provided github link can be used to view the files directly.\u003c/p\u003e\n"]]],[],null,["# Get a user\n\nMerchant API code sample to get a user. \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.users.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.GetUserRequest;\n import com.google.shopping.merchant.accounts.v1.User;\n import com.google.shopping.merchant.accounts.v1.UserName;\n import com.google.shopping.merchant.accounts.v1.UserServiceClient;\n import com.google.shopping.merchant.accounts.v1.UserServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /** This class demonstrates how to get a single user for a given Merchant Center account. */\n public class GetUserSample {\n\n public static void getUser(Config config, String email) 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 UserServiceSettings userServiceSettings =\n UserServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates user name to identify user.\n String name =\n UserName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setEmail(email)\n .build()\n .toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {\n\n // The name has the format: accounts/{account}/users/{email}\n GetUserRequest request = GetUserRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending Get user request:\");\n User response = userServiceClient.getUser(request);\n\n System.out.println(\"Retrieved User 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 // The email address of this user. If you want to get the user information\n // Of the user making the Content API request, you can also use \"me\" instead\n // Of an email address.\n String email = \"testUser@gmail.com\";\n\n getUser(config, email);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/users/v1/GetUserSample.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\n /**\n * Demonstrates how to get a single user for a given Merchant Center account.\n */\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\\GetUserRequest;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\UserServiceClient;\n\n /**\n * Retrieves a user.\n *\n * @param array $config The configuration data.\n * @param string $email The email address of the user.\n * @return void\n */\n function getUser($config, $email): 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 $userServiceClient = new UserServiceClient($options);\n\n // Creates user name to identify user.\n $name = 'accounts/' . $config['accountId'] . \"/users/\" . $email;\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n $request = new GetUserRequest(['name' =\u003e $name]);\n\n print \"Sending Get user request:\\n\";\n $response = $userServiceClient-\u003egetUser($request);\n\n print \"Retrieved User below\\n\";\n print_r($response);\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n $config = Config::generateConfig();\n $email = \"testUser@gmail.com\";\n\n getUser($config, $email); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/users/v1/GetUserSample.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 to get a user.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import GetUserRequest\n from google.shopping.merchant_accounts_v1 import UserServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def get_user(user_email):\n \"\"\"Gets a single user for a given Merchant Center account.\"\"\"\n\n # Get OAuth credentials\n credentials = generate_user_credentials.main()\n\n # Create a UserServiceClient\n client = UserServiceClient(credentials=credentials)\n\n # Create user name string\n name = \"accounts/\" + _ACCOUNT + \"/users/\" + user_email\n\n # Create the request\n request = GetUserRequest(name=name)\n\n try:\n print(\"Sending Get user request:\")\n response = client.get_user(request=request)\n print(\"Retrieved User below\")\n print(response)\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n # Modify this email to get the user details\n email = \"USER_MAIL_ACCOUNT\"\n get_user(email)\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/users/v1/get_user_sample.py"]]