میتوانید از API حسابهای تجاری برای کنترل اینکه چه کسانی به حساب تاجر شما دسترسی دارند و چه سطح دسترسی دارند، استفاده کنید.
User
شخصی است که به حساب تجاری شما دسترسی دارد. میتوانید از API حسابهای تجاری برای مشاهده، افزودن و حذف کاربران از حساب خود استفاده کنید.
یک کاربر به حساب خود اضافه کنید
برای افزودن کاربر، روش accounts.users.create
را فراخوانی کنید و سطح دسترسی آنها را در قسمت access_rights
User
مشخص کنید.
درخواست نمونه زیر نحوه افزودن یک کاربر جدید با مجوزهای STANDARD
و PERFORMANCE_REPORTING
را نشان می دهد:
POST https://merchantapi.googleapis.com/accounts/v1beta/accounts/{ACCOUNT_ID} /users?userId={USER_EMAILID}
{
"accessRights": [
"STANDARD",
"PERFORMANCE_REPORTING"
],
"name": "{NAME} "
}
موارد زیر را جایگزین کنید:
- {ACCOUNT_ID} : شناسه منحصر به فرد حساب Merchant Center شما.
- {USER_EMAILID} : آدرس ایمیل کاربری که میخواهید اضافه کنید.
- {NAME} : نام منبع کاربر در قالب
accounts/
{ACCOUNT_ID}/user/
{EMAIL_ADDRESS} .
پس از موفقیت آمیز بودن درخواست، پاسخ زیر برگردانده می شود:
{
"name": "accounts/{ACCOUNT_ID} /users/{USER_EMAILID} ",
"state": "PENDING",
"accessRights": [
"STANDARD",
"PERFORMANCE_REPORTING"
]
}
این درخواست دعوتنامه ای را به حساب Google مرتبط با کاربر جدید ارسال می کند، که باید پذیرفته شود تا کاربر به عنوان عضوی از حساب شما در نظر گرفته شود.
نمونه زیر نشان می دهد که چگونه می توانید از روش CreateUserRequest
برای افزودن کاربر به حساب خود استفاده کنید.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.AccessRight;
import com.google.shopping.merchant.accounts.v1beta.CreateUserRequest;
import com.google.shopping.merchant.accounts.v1beta.User;
import com.google.shopping.merchant.accounts.v1beta.UserServiceClient;
import com.google.shopping.merchant.accounts.v1beta.UserServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to create a user for a Merchant Center account. */
public class CreateUserSample {
private static String getParent(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void createUser(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 parent to identify where to insert the user.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
CreateUserRequest request =
CreateUserRequest.newBuilder()
.setParent(parent)
// This field is the email address of the user.
.setUserId(email)
.setUser(
User.newBuilder()
.addAccessRights(AccessRight.ADMIN)
.addAccessRights(AccessRight.PERFORMANCE_REPORTING)
.build())
.build();
System.out.println("Sending Create User request");
User response = userServiceClient.createUser(request);
System.out.println("Inserted User Name below");
// The last part of the user name will be the email address of the user.
// Format: `accounts/{account}/user/{user}`
System.out.println(response.getName());
} 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.
String email = "testUser@gmail.com";
createUser(config, email);
}
}
مشاهده کاربرانی که به حساب شما دسترسی دارند
برای مشاهده همه کاربرانی که به حساب شما دسترسی دارند، از روش accounts.users.list
استفاده کنید.
نمونه زیر نشان می دهد که چگونه می توانید از روش ListUsersRequest
برای فهرست کردن همه کاربران برای یک حساب معین استفاده کنید.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.ListUsersRequest;
import com.google.shopping.merchant.accounts.v1beta.User;
import com.google.shopping.merchant.accounts.v1beta.UserServiceClient;
import com.google.shopping.merchant.accounts.v1beta.UserServiceClient.ListUsersPagedResponse;
import com.google.shopping.merchant.accounts.v1beta.UserServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to list all the users for a given Merchant Center account. */
public class ListUsersSample {
private static String getParent(String accountId) {
return String.format("accounts/%s", accountId);
}
public static void listUsers(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.
UserServiceSettings userServiceSettings =
UserServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify the account from which to list all users.
String parent = getParent(config.getAccountId().toString());
// Calls the API and catches and prints any network failures/errors.
try (UserServiceClient userServiceClient = UserServiceClient.create(userServiceSettings)) {
// The parent has the format: accounts/{account}
ListUsersRequest request = ListUsersRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list users request:");
ListUsersPagedResponse response = userServiceClient.listUsers(request);
int count = 0;
// Iterates over all rows in all pages and prints the user
// in each row.
// `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
// request to fetch all pages of data.
for (User element : response.iterateAll()) {
System.out.println(element);
count++;
}
System.out.print("The following count of elements were returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listUsers(config);
}
}
یک کاربر برای یک حساب معین بازیابی کنید
نمونه زیر نشان می دهد که چگونه می توانید از روش GetUserRequest
برای بازیابی کاربر برای یک حساب معین استفاده کنید.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.GetUserRequest;
import com.google.shopping.merchant.accounts.v1beta.User;
import com.google.shopping.merchant.accounts.v1beta.UserName;
import com.google.shopping.merchant.accounts.v1beta.UserServiceClient;
import com.google.shopping.merchant.accounts.v1beta.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);
}
}
یک کاربر را از یک حساب مشخص حذف کنید
برای حذف یک کاربر از حساب خود، با استفاده از روش accounts.users.delete
درخواست دهید.
نمونه زیر نشان می دهد که چگونه می توانید از روش DeleteUserRequest
برای حذف یک کاربر از یک حساب معین استفاده کنید.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1beta.DeleteUserRequest;
import com.google.shopping.merchant.accounts.v1beta.UserName;
import com.google.shopping.merchant.accounts.v1beta.UserServiceClient;
import com.google.shopping.merchant.accounts.v1beta.UserServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to delete a user for a given Merchant Center account. */
public class DeleteUserSample {
public static void deleteUser(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 the 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)) {
DeleteUserRequest request = DeleteUserRequest.newBuilder().setName(name).build();
System.out.println("Sending Delete User request");
userServiceClient.deleteUser(request); // no response returned on success
System.out.println("Delete successful.");
} 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 delete 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";
deleteUser(config, email);
}
}
تغییر سطح دسترسی کاربر
برای تغییر سطح دسترسی کاربر، با روش accounts.users.patch
با سطح دسترسی جدید تماس بگیرید.
نمونه زیر نشان می دهد که چگونه می توانید از روش UpdateUserSample
برای به روز رسانی یک کاربر استفاده کنید تا آن را به عنوان مدیر یک حساب معین تبدیل کنید.
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.protobuf.FieldMask;
import com.google.shopping.merchant.accounts.v1beta.AccessRight;
import com.google.shopping.merchant.accounts.v1beta.UpdateUserRequest;
import com.google.shopping.merchant.accounts.v1beta.User;
import com.google.shopping.merchant.accounts.v1beta.UserName;
import com.google.shopping.merchant.accounts.v1beta.UserServiceClient;
import com.google.shopping.merchant.accounts.v1beta.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);
}
}
مقایسه بین super admin، admin و دسترسی استاندارد
کاربران با دسترسی فوقالعاده سرپرست به Business Manager را نمیتوان از Merchant Center حذف کرد. برای اطلاعات بیشتر درباره دسترسی فوقالعاده سرپرست، به مدیریت کسبوکارتان بهعنوان یک سوپر سرپرست مراجعه کنید.
برخی از روشها، مانند روشهایی که دادههای حساب را مینویسند، به دسترسی سرپرست نیاز دارند. برای یادگیری سطح دسترسی مورد نیاز برای هر روش، به مستندات مرجع مراجعه کنید.
اگر سطح دسترسی مشخص نشده است، می توانید از روش با دسترسی استاندارد استفاده کنید.
بعدش چی
- بیاموزید که چگونه می توانید حساب های فرعی ایجاد و مدیریت کنید .
- برای درک نحوه عملکرد روابط بین حسابهای تجاری، به روابط بین حسابها مراجعه کنید.