Trang này giải thích cú pháp mà bạn phải sử dụng để lọc tài khoản.
Cú pháp
Bạn phải đặt tất cả các giá trị không phải là số nguyên trong dấu ngoặc kép ("). Để tìm hiểu những giá trị mà một trường cụ thể chấp nhận, hãy xem tài liệu tham khảo cho trường đó.
Bạn có thể dùng AND
để lọc nhiều trường trong cùng một truy vấn. Bạn cũng có thể dùng AND
để kết hợp nhiều bộ lọc relationship(...)
và service(...)
.
Sau đây là ví dụ kết hợp nhiều bộ lọc relationship(...)
và service(...)
:
(relationship(service(type = "ACCOUNT_MANAGEMENT") AND service(handshakeState = "PENDING"))) OR (accountName = "store" AND relationship(...))
Ví dụ này trả về các tài khoản sau:
Tất cả tài khoản có mối quan hệ quản lý tài khoản với một tài khoản khác và một mối quan hệ bổ sung đang chờ được chấp nhận.
Tất cả tài khoản có tên hiển thị là
"store"
và có mối quan hệ với các tài khoản khác.
Bạn không thể dùng AND
để lọc nhiều giá trị trong cùng một trường. Ví dụ: bạn không thể dùng accountName = "*A*" AND accountName = "*B*"
.
Bạn có thể dùng OR
để lọc hai trường trong cùng một truy vấn. Đặt tiêu chí lọc ở mỗi bên của toán tử OR
trong dấu ngoặc đơn. Ví dụ: (accountName = "storeA") OR (accountName = "storeB")
Bạn chỉ có thể dùng OR
để kết hợp 2 trường. Ví dụ: bạn không thể sử dụng (accountName = "storeA") OR (accountName = "storeB") OR (accountName =
"storeC")
.
Bạn không được phép dùng dấu ngoặc đơn, trừ phi dùng với toán tử AND
và OR
, cũng như trong các lệnh gọi hàm, chẳng hạn như relationship(...)
và service(...)
.
Đối với các trường chuỗi như accountName
và accountIdAlias
, bạn có thể lọc các giá trị chứa một từ hoặc chuỗi ký tự nhất định bằng cách đặt chuỗi đó trong dấu hoa thị (*
). Ví dụ: accountName = "*foo*"
trả về tất cả tài khoản có accountName
chứa foo
, chẳng hạn như "storeFoo".
Bạn có thể lọc các giá trị không chứa một chuỗi nhất định bằng cách sử dụng !=
và *
. Ví dụ: accountName != "*foo*"
trả về tất cả tài khoản có accountName
không chứa foo
.
Khoảng trắng thừa sẽ bị bỏ qua. Ví dụ: foo AND bar
giống với foo
AND bar
.
Sau đây là một số ví dụ về cách lọc tài khoản bằng phương thức account.list:
"* Tất cả tài khoản phụ của tài khoản nhiều khách hàng có chứa "Cửa hàng"
accountName = "*store*" AND relationship(service(type = "ACCOUNT_AGGREGATION"))
- Tất cả tài khoản do nhà cung cấp 123456 quản lý
relationship(service(type = "ACCOUNT_MANAGEMENT") AND providerId = 123456)
- Tất cả tài khoản đã gửi lời mời đến nhà cung cấp 123456 hoặc cần chấp nhận lời mời của nhà cung cấp này
relationship(service(handshakeState = "PENDING" AND type ="ACCOUNT_MANAGEMENT")
AND providerId = 123456)
Để lọc những tài khoản mà người dùng đưa ra yêu cầu có thể truy cập, hãy sử dụng phương thức google.shopping.merchant.accounts.v1.ListAccountsRequest
, như trong mẫu sau.
Java
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.Account;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient;
import com.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListAccountsPagedResponse;
import com.google.shopping.merchant.accounts.v1.AccountsServiceSettings;
import com.google.shopping.merchant.accounts.v1.ListAccountsRequest;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/** This class demonstrates how to filter the accounts the user making the request has access to. */
public class FilterAccountsSample {
public static void filterAccounts(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.
AccountsServiceSettings accountsServiceSettings =
AccountsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Calls the API and catches and prints any network failures/errors.
try (AccountsServiceClient accountsServiceClient =
AccountsServiceClient.create(accountsServiceSettings)) {
// Filter for accounts with display names containing "store" and a provider with the ID "123":
String filter = "accountName = \"*store*\" AND relationship(providerId = 123)";
// Filter for all subaccounts of account "123":
// String filter2 = "relationship(providerId = 123 AND service(type =
// \"ACCOUNT_AGGREGATION\"))";
// String filter3 = "relationship(service(handshakeState = \"APPROVED\" AND type =
// \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";
ListAccountsRequest request = ListAccountsRequest.newBuilder().setFilter(filter).build();
System.out.println("Sending list accounts request with filter:");
ListAccountsPagedResponse response = accountsServiceClient.listAccounts(request);
int count = 0;
// Iterates over all rows in all pages and prints the sub-account
// in each row.
// `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
// request to fetch all pages of data.
for (Account account : response.iterateAll()) {
System.out.println(account);
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();
filterAccounts(config);
}
}
Thông số kỹ thuật
Các bộ lọc tuân theo một nhóm nhỏ trong quy cách bộ lọc AIP và ngữ pháp EBNF chính thức của bộ lọc đó:
filter
: accountFilterDisj
| accountFilterConj
accountFilterDisj
: "(" accountFilterConj " OR " accountFilterConj ")"
;
accountFilterConj
: accountFilter {" AND " accountFilter}
;
accountFilter
: accountNameFilter | capabilityFilter | relationshipFn
;
accountNameFilter
: "accountName" comparator value
;
capabilityFilter
: "capabilities:" capabilityValue
| "-capabilities:" capabilityValue
| "NOT capabilities:" capabilityValue
;
capabilityValue
: "CAN_UPLOAD_PRODUCTS"
;
relationshipFn
: "relationship(" relationshipConj ")"
;
relationshipConj
: relationshipFilter {" AND " relationshipFilter}
;
relationshipFilter
: "providerId = " numValue
| "accountIdAlias" comparator value
| serviceFn
;
serviceFn
: "service(" serviceConj ")"
;
serviceConj
: serviceFilter {" AND " serviceFilter}
;
serviceFilter
: "externalAccountId" comparator value
| "handshakeState = " handshakeState
| "type = " serviceType
;
handshakeState
: "PENDING"
| "APPROVED"
| "REJECTED"
;
serviceType
: "ACCOUNT_AGGREGATION"
| "ACCOUNT_MANAGEMENT"
;
comparator
: " = " | " != "
;