直接 API 呼叫

採用這種方式與 Merchant API 互動時,通常會透過 HTTP(S) 直接向 API 端點發出原始要求。您必須手動處理要求主體的形成、標頭設定 (包括驗證) 和回應剖析等層面。

步驟 1:註冊成為開發人員

如要使用 Merchant API,請務必註冊開發人員聯絡資訊。

註冊後即可:

  • API developer 角色指派給使用者,為 Merchant Center 帳戶建立技術聯絡人。這樣一來,Google 就能傳送與 API 和開發人員使用的功能相關的重要更新,例如服務公告和新功能資訊,這些資訊對非開發人員來說可能較不重要。
  • 可讓您使用多個商家帳戶,不必重複註冊。註冊時,用於向 Merchant API 驗證的 Google Cloud 專案 ID 會與 Merchant Center 帳戶建立關聯,該帳戶具有技術聯絡人 (即 API developer)。這樣一來,只要使用已註冊的 Google Cloud 專案完成驗證,您就能接收所管理所有商家帳戶的重要更新。

註冊時,請遵守「註冊」頁面詳述的先決條件和限制。

如要使用 developerRegistration.registerGcp 方法註冊,請先參閱這個範例,並將 {DEVELOPER_EMAIL} 替換為要求內文中的適當電子郵件地址。

POST https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}/developerRegistration:registerGcp

{
  "developerEmail": "{DEVELOPER_EMAIL}"
}

如果呼叫成功,會傳回 DeveloperRegistration 資源,確認專案與帳戶已連結。

{
  "name": "accounts/{ACCOUNT_ID}/developerRegistration",
  "gcpIds": [
    "123456789012345"
  ]
}

步驟 2:管理開發人員聯絡人和權限

註冊時:

  • 如果電子郵件地址屬於 Merchant Center 帳戶中的使用者,系統會授予該使用者 API_DEVELOPER 角色。
  • 如果該電子郵件地址不屬於現有使用者,系統會將邀請傳送至該地址。收件者必須接受邀請,才能以 API_DEVELOPER 角色新增為使用者。

完成初始註冊後,建議您新增多位開發人員,並授予他們額外的存取權。

步驟 2a. 授予其他權限

如要接收重要通知,必須具備API_DEVELOPER角色,但這項角色在 Merchant Center 中的權限最少。如要允許這位使用者發出其他 API 呼叫或在 Merchant Center 使用者介面中管理設定,您必須授予他們其他角色,例如STANDARDADMIN。詳情請參閱「存取類型」。

您可以使用 accounts.users.patch 方法更新使用者的存取權。

以下範例說明如何更新使用者,同時授予 ADMINAPI_DEVELOPER 角色。這樣一來,他們就能全權管理帳戶,並接收 API 相關通訊內容。

PATCH https://merchantapi.googleapis.com/accounts/v1/accounts/{ACCOUNT_ID}/users/{DEVELOPER_EMAIL}?update_mask=access_rights
{
  "access_rights": [
    "ADMIN",
    "API_DEVELOPER"
  ]
}

步驟 2b:新增備用開發人員

為避免主要開發人員聯絡人離開機構後,API 存取權受到影響,建議您至少新增一位備用開發人員。

您可以使用 accounts.users.create 方法新增使用者,或使用 accounts.users.patch 方法更新使用者。建議您同時指派 ADMINAPI_DEVELOPER 角色給這位使用者。

步驟 3:建立主要產品資料來源

如要插入產品,必須有主要產品資料來源。以下要求顯示如何建立資料來源,以便將產品插入帳戶:

POST https://merchantapi.googleapis.com/datasources/v1/accounts/{ACCOUNT_ID}/dataSources

{
  "primaryProductDataSource": {
    "contentLanguage": "en",
    "countries": [
      "US"
    ],
    "feedLabel": "US"
  },
  "name": "primary-data-source",
  "displayName": "Primary Products Data Source"
}

{ACCOUNT_ID} 替換為您建立的 Merchant Center 帳戶 ID。

成功執行這項要求後,您應該會看到以下回應:

{
  "name": "accounts/{ACCOUNT_ID}/dataSources/{DATASOURCE_ID}",
  "dataSourceId": "{DATASOURCE_ID}",
  "displayName": "Primary Products Data Source",
  "primaryProductDataSource": {
    "feedLabel": "US",
    "contentLanguage": "en",
    "countries": [
      "US"
    ],
    "defaultRule": {
      "takeFromDataSources": [
        {
          "self": true
        }
      ]
    }
  },
  "input": "API"
}

複製 name 欄位的值。插入產品時需要用到這項資訊。

你可以在 Merchant Center 使用者介面中查看這個資料來源。詳情請參閱「如何找到『資料來源』分頁」。

步驟 4:插入一項產品

建立資料來源後,請嘗試將產品插入其中。執行下列指令,並提供正確的 ACCOUNT_ID。將 {DATASOURCE_NAME} 換成您先前複製的值。

POST https://merchantapi.googleapis.com/products/v1/accounts/{ACCOUNT_ID}/productInputs:insert?dataSource={DATASOURCE_NAME}
{
  "contentLanguage": "en",
  "feedLabel": "US",
  "name": "Red T-shirt",
  "productAttributes": {
    "gender": "MALE",
    "brand": "New brand"
  },
  "offerId": "tshirt-123"
}

成功執行這項要求後,您應該會看到以下回應:

{
  "name": "accounts/{ACCOUNT_ID}/productInputs/en~US~tshirt-123",
  "product": "accounts/{ACCOUNT_ID}/products/en~US~tshirt-123",
  "offerId": "tshirt-123",
  "contentLanguage": "en",
  "feedLabel": "US",
  "productAttributes": {
    "brand": "New brand",
    "gender": "MALE"
  }
}

新建立的產品 ID 為 en~US~tshirt-123。您可以使用 accounts.products.get 方法擷取這項產品的詳細資料。你也可以使用 Merchant Center 使用者介面查看這項產品。請參閱「查看產品資料」。