Stay organized with collectionsSave and categorize content based on your preferences.
Merchant API code sample to update a user.
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.users.v1;importcom.google.api.gax.core.FixedCredentialsProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.protobuf.FieldMask;importcom.google.shopping.merchant.accounts.v1.AccessRight;importcom.google.shopping.merchant.accounts.v1.UpdateUserRequest;importcom.google.shopping.merchant.accounts.v1.User;importcom.google.shopping.merchant.accounts.v1.UserName;importcom.google.shopping.merchant.accounts.v1.UserServiceClient;importcom.google.shopping.merchant.accounts.v1.UserServiceSettings;importshopping.merchant.samples.utils.Authenticator;importshopping.merchant.samples.utils.Config;/** This class demonstrates how to update a user to make it an admin of the MC account. */publicclassUpdateUserSample{publicstaticvoidupdateUser(Configconfig,Stringemail,AccessRightaccessRight)throwsException{GoogleCredentialscredential=newAuthenticator().authenticate();UserServiceSettingsuserServiceSettings=UserServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Creates user name to identify user.Stringname=UserName.newBuilder().setAccount(config.getAccountId().toString()).setEmail(email).build().toString();// Create a user with the updated fields.Useruser=User.newBuilder().setName(name).addAccessRights(accessRight).build();FieldMaskfieldMask=FieldMask.newBuilder().addPaths("access_rights").build();try(UserServiceClientuserServiceClient=UserServiceClient.create(userServiceSettings)){UpdateUserRequestrequest=UpdateUserRequest.newBuilder().setUser(user).setUpdateMask(fieldMask).build();System.out.println("Sending Update User request");Userresponse=userServiceClient.updateUser(request);System.out.println("Updated User Name below");System.out.println(response.getName());}catch(Exceptione){System.out.println(e);}}publicstaticvoidmain(String[]args)throwsException{Configconfig=Config.load();Stringemail="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.AccessRightaccessRight=AccessRight.ADMIN;updateUser(config,email,accessRight);}}
<?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);
# -*- 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."""fromexamples.authenticationimportconfigurationfromexamples.authenticationimportgenerate_user_credentialsfromgoogle.protobufimportfield_mask_pb2fromgoogle.shopping.merchant_accounts_v1importAccessRightfromgoogle.shopping.merchant_accounts_v1importUpdateUserRequestfromgoogle.shopping.merchant_accounts_v1importUserfromgoogle.shopping.merchant_accounts_v1importUserServiceClientFieldMask=field_mask_pb2.FieldMask_ACCOUNT=configuration.Configuration().read_merchant_info()defupdate_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 stringname="accounts/"+_ACCOUNT+"/users/"+user_emailuser=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)exceptRuntimeErrorase:print(e)if__name__=="__main__":# Modify this email to update the right useremail="USER_MAIL_ACCOUNT"access_right=AccessRight.ADMINupdate_user(email,access_right)
[[["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 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"]]