เมธอด accounts.list
ช่วยให้คุณดึงข้อมูลรายการทรัพยากร Account
ที่ผู้ใช้ที่ตรวจสอบสิทธิ์แล้วเข้าถึงได้ คุณสามารถใช้พารามิเตอร์การค้นหา filter
เพื่อ
จำกัดผลลัพธ์ตามเกณฑ์ต่างๆ เช่น
- พร็อพเพอร์ตี้บัญชี
- ความสัมพันธ์กับบัญชีอื่นๆ (เช่น ผู้ให้บริการในโครงสร้างบัญชีขั้นสูง )
- บริการที่เชื่อมโยงกับบัญชี
ซึ่งจะเป็นประโยชน์ในการจัดการหลายบัญชีหรือค้นหาบัญชีธุรกิจที่เฉพาะเจาะจงซึ่งเป็นไปตามเงื่อนไขบางอย่าง
คุณใช้ช่องต่อไปนี้เพื่อกรองที่ระดับ account
ได้
access
: กรองตามประเภทการเข้าถึงที่ผู้ใช้มีต่อaccount
ฟิลเตอร์นี้ ยอมรับค่าต่อไปนี้DIRECT
: แสดงเฉพาะบัญชีที่ผู้ใช้มีสิทธิ์เข้าถึงโดยตรงINDIRECT
: แสดงเฉพาะบัญชีที่ผู้ใช้มีสิทธิ์เข้าถึงโดยอ้อมALL
: แสดงบัญชีทั้งหมดที่ผู้ใช้มีสิทธิ์เข้าถึง (ทั้งโดยตรงและ โดยอ้อม) ซึ่งเป็นลักษณะการทำงานเริ่มต้นหากไม่ได้ระบุตัวกรอง
capabilities
: กรองตามcapabilities
ของทรัพยากรaccount
(โปรดทราบว่าฟิลด์นี้ไม่พร้อมใช้งานในทรัพยากรเอง) รองรับเฉพาะความสามารถของCAN_UPLOAD_PRODUCTS
ฟิลด์นี้รองรับการปฏิเสธ และใช้ไวยากรณ์ของคอลเล็กชันrelationship(...)
: กรองตามประเภทความสัมพันธ์ที่บัญชีมีกับบัญชีอื่น คุณใส่ตัวกรองrelationship(...)
หลายรายการ ในคำขอเดียวได้accountName
: กรองตามaccountName
ของทรัพยากรaccount
ดูข้อมูลโดยละเอียดเพิ่มเติมเกี่ยวกับไวยากรณ์ของตัวกรองได้ที่คำแนะนำเกี่ยวกับไวยากรณ์ของตัวกรอง
ตัวอย่าง
ตัวอย่างต่อไปนี้อธิบายวิธีสร้างคำค้นหาที่พบบ่อยที่สุด ตัวอย่างต่อไปนี้ทั้งหมดใช้วิธี accounts.list
ดูข้อมูลเพิ่มเติมได้ที่
accounts.list
เอกสารอ้างอิง
ค้นหาบัญชีย่อยของผู้ให้บริการที่เฉพาะเจาะจง
เมธอด
accounts.listSubaccounts
เป็นวิธีโดยตรงในการแสดงบัญชีย่อย นอกจากนี้ คุณยังใช้
ความสามารถในการกรองตามที่อธิบายไว้ในส่วนต่อไปนี้ได้ด้วย หากจัดการ
บัญชีขั้นสูง คุณจะแสดงบัญชีย่อยทั้งหมดได้โดยการกรองใน
providerId
แทนที่ PROVIDER_ID
ด้วยรหัสของ
บัญชีขั้นสูง
เช่น ใช้ relationship(providerId=123)
หากรหัสของผู้ให้บริการคือ
123
ซึ่งจะมีประโยชน์ในการจัดการโครงสร้างของบัญชี
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=relationship(providerId%20%3D%20PROVIDER_ID)
คำขอที่สำเร็จจะแสดงรหัสสถานะ 200 และเนื้อหาการตอบกลับพร้อมรายการ บัญชีย่อยที่ตรงกัน
{
"accounts": [
{
"name": "accounts/77777",
"accountId": "77777",
"accountName": "SubAccount A of Provider",
"adultContent": false,
"languageCode": "fr",
"timeZone": {
"id": "Europe/Paris"
}
},
{
"name": "accounts/88888",
"accountId": "88888",
"accountName": "SubAccount B of Provider",
"adultContent": false,
"languageCode": "de",
"timeZone": {
"id": "Europe/Berlin"
}
}
],
"nextPageToken": "XYZ123abcDEF..."
}
ค้นหาบัญชีที่อัปโหลดผลิตภัณฑ์ไม่ได้
คุณสามารถรวมเงื่อนไขตัวกรองหลายรายการเพื่อสร้างการค้นหาที่เฉพาะเจาะจงยิ่งขึ้น
ฟิลเตอร์ accountName=*store* AND -capabilities:CAN_UPLOAD_PRODUCTS
จะค้นหาบัญชีทั้งหมด
ที่มี "store" ในชื่อซึ่งไม่ได้กำหนดค่าให้อัปโหลดผลิตภัณฑ์
โดยตรง -
ก่อน capabilities
ทำหน้าที่เป็นตัวดำเนินการนิเสธ ซึ่งจะเป็นประโยชน์ในการดึงข้อมูลเฉพาะบัญชีขั้นสูง
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=accountName%20%3D%20%22*store*%22%20AND%20-capabilities%3ACAN_UPLOAD_PRODUCTS
คำขอที่สำเร็จจะแสดงรหัสสถานะ 200 และเนื้อหาการตอบกลับพร้อมรายการ บัญชีที่ตรงกัน
{
"accounts": [
{
"name": "accounts/54321",
"accountId": "54321",
"accountName": "Partner Store - US",
"adultContent": false,
"languageCode": "en",
"timeZone": {
"id": "America/New_York"
}
},
{
"name": "accounts/98765",
"accountId": "98765",
"accountName": "Auxiliary Brand Store",
"adultContent": false,
"languageCode": "fr",
"timeZone": {
"id": "Europe/Paris"
}
}
],
"nextPageToken": "CDEfghIJKlmnOPQ..."
}
ค้นหาบัญชีตามชื่อ
คุณค้นหาบัญชีที่มีชื่อที่แสดงตรงกับรูปแบบที่เฉพาะเจาะจงได้
เช่น accountName=*store*
จะค้นหาบัญชีทั้งหมดที่มีคำว่า "store" ในชื่อ
ซึ่งจะช่วยให้ค้นหาบัญชีธุรกิจที่เฉพาะเจาะจงได้อย่างรวดเร็ว
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=accountName%20%3D%20%22*store*%22
คำขอที่สำเร็จจะแสดงรหัสสถานะ 200 และเนื้อหาการตอบกลับพร้อมรายการ บัญชีที่ตรงกัน
{
"accounts": [
{
"name": "accounts/12345",
"accountId": "12345",
"accountName": "My Awesome Store",
"adultContent": false,
"languageCode": "en",
"timeZone": {
"id": "America/Los_Angeles"
}
},
{
"name": "accounts/67890",
"accountId": "67890",
"accountName": "Another Store Online",
"adultContent": false,
"languageCode": "en",
"timeZone": {
"id": "Europe/London"
}
}
],
"nextPageToken": "ABSdefGHIjklMNO..."
}
ค้นหาบัญชีที่ลิงก์กับผู้ให้บริการสำหรับบริการที่เฉพาะเจาะจง
คุณค้นหาบัญชีที่มีความสัมพันธ์ด้านบริการที่เฉพาะเจาะจงกับผู้ให้บริการได้
เช่น หากต้องการค้นหาบัญชีทั้งหมดที่รวบรวมไว้ภายใต้ผู้ให้บริการ PROVIDER_ID
สำหรับการรวบรวมบัญชี ให้ใช้ตัวกรอง
relationship(providerId=PROVIDER_ID) AND
service(type="ACCOUNT_AGGREGATION")
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=relationship(providerId%20%3D%20PROVIDER_ID%20AND%20service(type%20%3D%20%22ACCOUNT_AGGREGATION%22))
คำขอที่สำเร็จจะแสดงรหัสสถานะ 200 และเนื้อหาการตอบกลับพร้อมรายการ บัญชีที่ตรงกัน
{
"accounts": [
{
"name": "accounts/54321",
"accountId": "54321",
"accountName": "Aggregated Account X",
"adultContent": false,
"languageCode": "en",
"timeZone": {
"id": "America/New_York"
}
}
]
}
ค้นหาบัญชีตามสถานะการอนุมัติความสัมพันธ์ของบริการ
คุณสามารถกรองบัญชีตามสถานะความสัมพันธ์ด้านบริการกับผู้ให้บริการได้ เช่น หากต้องการค้นหาบัญชีทั้งหมดที่ยังไม่ยอมรับคำขอลิงก์บัญชี(handshakeState = "PENDING")
จากผู้ให้บริการรายหนึ่งPROVIDER_ID
เช่น หากต้องการค้นหาบัญชีที่มีรหัสผู้ให้บริการเป็น 123
ประเภทบริการเป็น ACCOUNT_MANAGEMENT
และสถานะเป็น PENDING
ให้ใช้ relationship(service(handshakeState = "PENDING" AND type =
"ACCOUNT_MANAGEMENT") AND providerId = 123)
GET https://merchantapi.googleapis.com/accounts/v1/accounts?filter=relationship(service(handshakeState%20%3D%20%22PENDING%22%20AND%20type%20%3D%20%22ACCOUNT_MANAGEMENT%22)%20AND%20providerId%20%3D%20PROVIDER_ID)
คำขอที่สำเร็จจะแสดงรหัสสถานะ 200 และเนื้อหาการตอบกลับพร้อมรายการ บัญชีที่ตรงกัน
{
"accounts": [
{
"name": "accounts/98765",
"accountId": "98765",
"accountName": "Managed Account Y",
"adultContent": false,
"languageCode": "es",
"timeZone": {
"id": "Europe/Madrid"
}
}
]
}
กรองบัญชีโดยใช้ไลบรารีของไคลเอ็นต์
ตัวอย่างต่อไปนี้แสดงวิธีใช้ไลบรารีของไคลเอ็นต์เพื่อกรองบัญชีตามเกณฑ์ที่รวมกัน เช่น ชื่อบัญชีและความสัมพันธ์กับผู้ให้บริการ ตัวอย่างเหล่านี้ใช้วิธี accounts.list
ดูข้อมูลเพิ่มเติมได้ที่
เอกสารอ้างอิง
accounts.list
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);
}
}
PHP
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Accounts\V1\Client\AccountsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\ListAccountsRequest;
/**
* This class demonstrates how to filter the accounts the user making the request has access to.
*/
class FilterAccounts
{
public static function filterAccounts(array $config): 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.
$accountsServiceClient = new AccountsServiceClient($options);
// Calls the API and catches and prints any network failures/errors.
try {
// Filter for accounts with display names containing "store" and a provider with the ID "123":
$filter = "accountName = \"*store*\" AND relationship(providerId = 123)";
// Filter for all subaccounts of account "123":
// $filter = "relationship(providerId = 123 AND service(type = \"ACCOUNT_AGGREGATION\"))";
// $filter = "relationship(service(handshakeState = \"APPROVED\" AND type =
// \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";
$request = new ListAccountsRequest(['filter' => $filter]);
print "Sending list accounts request with filter:\n";
$response = $accountsServiceClient->listAccounts($request);
$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.
foreach ($response->iterateAllElements() as $account) {
print_r($account);
$count++;
}
print "The following count of elements were returned: ";
print $count . PHP_EOL;
} catch (ApiException $e) {
print $e->getMessage();
}
}
public function callSample(): void
{
$config = Config::generateConfig();
self::filterAccounts($config);
}
}
$sample = new FilterAccounts();
$sample->callSample();
Python
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import AccountsServiceClient
from google.shopping.merchant_accounts_v1 import ListAccountsRequest
def filter_accounts():
"""Filters the accounts the user making the request has access to."""
# Get OAuth credentials.
credentials = generate_user_credentials.main()
# Create a client.
client = AccountsServiceClient(credentials=credentials)
# Create the filter string.
filter_string = 'accountName = "*store*" AND relationship(providerId = 123)'
# Create the request.
request = ListAccountsRequest(filter=filter_string)
# Make the request and print the response.
try:
print("Sending list accounts request with filter:")
response = client.list_accounts(request=request)
count = 0
for account in response:
print(account)
count += 1
print(f"The following count of elements were returned: {count}")
except RuntimeError as e:
print(e)
if __name__ == "__main__":
filter_accounts()
AppsScript
/**
* Filters and lists accounts for which the logged-in user has access to
*/
function filterAccounts() {
// IMPORTANT:
// Enable the Merchant API Accounts sub-API Advanced Service and call it
// "MerchantApiAccounts"
// Create the filter string.
// Documentation can be found at
// https://developers.google.com/merchant/api/guides/accounts/filter-syntax
const filter = 'accountName = "*store*" AND relationship(providerId = 123)';
try {
console.log('Sending filter Accounts request');
let pageToken;
let pageSize = 500;
// Call the Accounts.list API method with a filter. Use the pageToken to iterate through
// all pages of results.
do {
response =
MerchantApiAccounts.Accounts.list({pageSize, pageToken, filter});
for (const account of response.accounts) {
console.log(account);
}
pageToken = response.nextPageToken;
} while (pageToken); // Exits when there is no next page token.
} catch (e) {
console.log('ERROR!');
console.log(e);
}
}
cURL
curl --location 'https://merchantapi.googleapis.com/accounts/v1/accounts?filter=accountName%20%3D%20%22*store*%22%20AND%20relationship(providerId%20%3D%20PROVIDER_ID)' \
--header 'Authorization: Bearer <API_TOKEN>'