تعديل مستخدم
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
نموذج رمز 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.protobuf.FieldMask;
import com.google.shopping.merchant.accounts.v1.AccessRight;
import com.google.shopping.merchant.accounts.v1.UpdateUserRequest;
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 update a user to make it an admin of the MC account. */
public class UpdateUserSample {
public static void updateUser(Config config, String email, AccessRight accessRight)
throws Exception {
GoogleCredentials credential = new Authenticator().authenticate();
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();
// Create a user with the updated fields.
User user = User.newBuilder().setName(name).addAccessRights(accessRight).build();
FieldMask fieldMask = FieldMask.newBuilder().addPaths("access_rights").build();
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
UpdateUserRequest request =
UpdateUserRequest.newBuilder().setUser(user).setUpdateMask(fieldMask).build();
System.out.println("Sending Update User request");
User response = userServiceClient.updateUser(request);
System.out.println("Updated User Name below");
System.out.println(response.getName());
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
String email = "testUser@gmail.com";
// Give the user admin rights. Note that all other rights, like
// PERFORMANCE_REPORTING, would be overwritten in this example
// if the user had those access rights before the update.
AccessRight accessRight = AccessRight.ADMIN;
updateUser(config, email, accessRight);
}
}
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 update a user to make it an admin of the MC 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\Protobuf\FieldMask;
use Google\Shopping\Merchant\Accounts\V1\AccessRight;
use Google\Shopping\Merchant\Accounts\V1\UpdateUserRequest;
use Google\Shopping\Merchant\Accounts\V1\User;
use Google\Shopping\Merchant\Accounts\V1\Client\UserServiceClient;
/**
* Updates a user.
*
* @param array $config The configuration data.
* @param string $email The email address of the user.
* @param int $accessRight The access right to grant the user.
* @return void
*/
function updateUser($config, $email, $accessRights): 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;
$user = (new User())
->setName($name)
->setAccessRights($accessRights);
$fieldMask = (new FieldMask())->setPaths(['access_rights']);
// Calls the API and catches and prints any network failures/errors.
try {
$request = new UpdateUserRequest([
'user' => $user,
'update_mask' => $fieldMask,
]);
print "Sending Update User request\n";
$response = $userServiceClient->updateUser($request);
print "Updated User Name below\n";
print $response->getName() . "\n";
} catch (ApiException $e) {
print $e->getMessage();
}
}
$config = Config::generateConfig();
$email = "testUser@gmail.com";
$accessRights = [AccessRight::ADMIN];
updateUser($config, $email, $accessRights);
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 update a user."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.protobuf import field_mask_pb2
from google.shopping.merchant_accounts_v1 import AccessRight
from google.shopping.merchant_accounts_v1 import UpdateUserRequest
from google.shopping.merchant_accounts_v1 import User
from google.shopping.merchant_accounts_v1 import UserServiceClient
FieldMask = field_mask_pb2.FieldMask
_ACCOUNT = configuration.Configuration().read_merchant_info()
def update_user(user_email, user_access_right):
"""Updates a user to make it an admin of the MC account."""
credentials = generate_user_credentials.main()
client = UserServiceClient(credentials=credentials)
# Create user name string
name = "accounts/" + _ACCOUNT + "/users/" + user_email
user = User(name=name, access_rights=[user_access_right])
field_mask = FieldMask(paths=["access_rights"])
try:
request = UpdateUserRequest(user=user, update_mask=field_mask)
print("Sending Update User request")
response = client.update_user(request=request)
print("Updated User Name below")
print(response.name)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
# Modify this email to update the right user
email = "USER_MAIL_ACCOUNT"
access_right = AccessRight.ADMIN
update_user(email, access_right)
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 2.0. للاطّلاع على التفاصيل، يُرجى مراجعة سياسات موقع Google Developers. إنّ Java هي علامة تجارية مسجَّلة لشركة Oracle و/أو شركائها التابعين.
تاريخ التعديل الأخير: 2025-08-21 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-21 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eThis webpage provides code samples in Java, PHP, and Python demonstrating how to update a user's access rights within a Merchant Center account, specifically to grant admin privileges.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples utilize the Merchant Center API and require authentication, showcasing how to set up credentials and create a \u003ccode\u003eUserServiceClient\u003c/code\u003e to interact with the API.\u003c/p\u003e\n"],["\u003cp\u003eEach example demonstrates the creation of a \u003ccode\u003eUser\u003c/code\u003e object with a specified name and access rights, alongside a \u003ccode\u003eFieldMask\u003c/code\u003e to indicate which fields to update.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eupdateUser\u003c/code\u003e function is utilized to make the request, passing the user object and update mask, and the response includes the updated user's name, confirming the modification.\u003c/p\u003e\n"],["\u003cp\u003eThe examples are all tied to a specific version of the library, with links to the specific github repository version.\u003c/p\u003e\n"]]],[],null,["# Update a user\n\nMerchant API code sample to update 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.protobuf.FieldMask;\n import com.google.shopping.merchant.accounts.v1.AccessRight;\n import com.google.shopping.merchant.accounts.v1.UpdateUserRequest;\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 update a user to make it an admin of the MC account. */\n public class UpdateUserSample {\n\n public static void updateUser(Config config, String email, AccessRight accessRight)\n throws Exception {\n\n GoogleCredentials credential = new Authenticator().authenticate();\n\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 // Create a user with the updated fields.\n User user = User.newBuilder().setName(name).addAccessRights(accessRight).build();\n\n FieldMask fieldMask = FieldMask.newBuilder().addPaths(\"access_rights\").build();\n\n try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {\n\n UpdateUserRequest request =\n UpdateUserRequest.newBuilder().setUser(user).setUpdateMask(fieldMask).build();\n\n System.out.println(\"Sending Update User request\");\n User response = userServiceClient.updateUser(request);\n System.out.println(\"Updated User Name below\");\n System.out.println(response.getName());\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 String email = \"testUser@gmail.com\";\n // Give the user admin rights. Note that all other rights, like\n // PERFORMANCE_REPORTING, would be overwritten in this example\n // if the user had those access rights before the update.\n AccessRight accessRight = AccessRight.ADMIN;\n\n updateUser(config, email, accessRight);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/users/v1/UpdateUserSample.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 update a user to make it an admin of the MC 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\\Protobuf\\FieldMask;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\AccessRight;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\UpdateUserRequest;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\User;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\UserServiceClient;\n\n\n /**\n * Updates a user.\n *\n * @param array $config The configuration data.\n * @param string $email The email address of the user.\n * @param int $accessRight The access right to grant the user.\n * @return void\n */\n function updateUser($config, $email, $accessRights): 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 $user = (new User())\n -\u003esetName($name)\n -\u003esetAccessRights($accessRights);\n\n $fieldMask = (new FieldMask())-\u003esetPaths(['access_rights']);\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n $request = new UpdateUserRequest([\n 'user' =\u003e $user,\n 'update_mask' =\u003e $fieldMask,\n ]);\n\n print \"Sending Update User request\\n\";\n $response = $userServiceClient-\u003eupdateUser($request);\n print \"Updated User Name below\\n\";\n print $response-\u003egetName() . \"\\n\";\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n\n $config = Config::generateConfig();\n $email = \"testUser@gmail.com\";\n $accessRights = [AccessRight::ADMIN];\n\n updateUser($config, $email, $accessRights); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/users/v1/UpdateUserSample.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 update a user.\"\"\"\n\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.protobuf import field_mask_pb2\n from google.shopping.merchant_accounts_v1 import AccessRight\n from google.shopping.merchant_accounts_v1 import UpdateUserRequest\n from google.shopping.merchant_accounts_v1 import User\n from google.shopping.merchant_accounts_v1 import UserServiceClient\n\n FieldMask = field_mask_pb2.FieldMask\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def update_user(user_email, user_access_right):\n \"\"\"Updates a user to make it an admin of the MC account.\"\"\"\n\n credentials = generate_user_credentials.main()\n\n client = UserServiceClient(credentials=credentials)\n\n # Create user name string\n name = \"accounts/\" + _ACCOUNT + \"/users/\" + user_email\n\n user = User(name=name, access_rights=[user_access_right])\n\n field_mask = FieldMask(paths=[\"access_rights\"])\n\n try:\n request = UpdateUserRequest(user=user, update_mask=field_mask)\n\n print(\"Sending Update User request\")\n response = client.update_user(request=request)\n print(\"Updated User Name below\")\n print(response.name)\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n # Modify this email to update the right user\n email = \"USER_MAIL_ACCOUNT\"\n access_right = AccessRight.ADMIN\n update_user(email, access_right)\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/users/v1/update_user_sample.py"]]