應用程式政策的程式碼範例

下列要求會以應用程式政策為例,說明透過 Policy API 管理政策的方式。在開始之前,請務必詳閱 Chrome Policy API 總覽政策結構定義指南

下方顯示的所有要求都使用下列變數:

  • $TOKEN:OAuth 2 權杖
  • $CUSTOMER - 客戶 ID 或常值my_customer

強制安裝應用程式

以下範例適用於機構單位。群組要求將相同,但 targetResource 除外,ID 之前為「groups/」,而非「orgunits/」。

以下設定為強制安裝 Google 繪圖應用程式:

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        requests: [{
                policyTargetKey: {
                        targetResource: "orgunits/04fatzly4jbjho9",
                        additionalTargetKeys: {"app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"}
                        },
                policyValue: {
                        policySchema: "chrome.users.apps.InstallType",
                        value: {appInstallType: "FORCED"}
                        },
                updateMask: {paths: "appInstallType"}
                }]
      }' \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/orgunits:batchModify"

成功的回應應該空白:

{}

本例適用於使用者應用程式。受管理的訪客工作階段要求會相同,但政策結構定義為 chrome.devices.managedguest.apps.InstallType。相應的資訊站應用程式政策要求如下:

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        requests: [{
                policyTargetKey: {
                        targetResource: "orgunits/04fatzly4jbjho9",
                        additionalTargetKeys: {"app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"}
                        },
                policyValue: {
                        policySchema: "chrome.devices.kiosk.apps.ForceInstall",
                        value: {forceInstall: false}
                        },
                updateMask: {paths: "forceInstall"}
                }]
      }' \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/orgunits:batchModify"

成功的回應應該空白:

{}

刪除應用程式

應用程式的刪除操作必須在明確新增應用程式的機構單位進行管理。呼叫解析時,addedSourceKey 欄位會包含您為了管理而新增的機構單位。也就是說,應用程式只有在 addedSourceKey 中的機構單位等於 policyTargetKey 中的機構單位呼叫 delete 時,才能呼叫 delete

如要刪除應用程式 (從管理中明確移除),您應傳送 batchInherit 要求,其中 policySchema 是特定應用程式類型的結構定義,並以星號 (*) 取代特定政策。在此範例中,我們會刪除安裝在「04fatzly4jbjho9」機構單位的 Google 繪圖應用程式:

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        requests: [{
                policyTargetKey: {
                        targetResource: "orgunits/04fatzly4jbjho9",
                        additionalTargetKeys: {"app_id": "chrome:gbchcmhmhahfdphkhkmpfmihenigjmpp"}
                        },
                policySchema: "chrome.users.apps.*",
                }]
      }' \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/orgunits:batchInherit"

成功的回應應該空白:

{}

刪除資訊站應用程式和代管訪客工作階段應用程式的要求會分別使用 chrome.devices.kiosk.apps.\*chrome.devices.managedguest.apps.\* 結構定義。

取得機構單位中的應用程式安裝政策

如要取得特定應用程式的政策,您必須在要求中同時指定政策和應用程式 ID。在這個範例中,我們是讀取 Google 繪圖應用程式的 InstallType 政策值:

要求

  curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        policyTargetKey: {
          targetResource: "orgunits/04fatzly4jbjho9",
          additionalTargetKeys: {"app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"}
        },
        policySchemaFilter: "chrome.users.apps.InstallType"
    }' \
  "https://chromepolicy.googleapis.com/v1/$CUSTOMER/C02l1chq7/policies:resolve"

回應

{
  "resolvedPolicies": [
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.InstallType",
        "value": {
          "appInstallType": "FORCED"
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    }
  ]
}

取得機構單位中所有應用程式的安裝政策

如果上一個範例的要求中省略應用程式 ID,則該機構單位下所有應用程式的 InstallType 政策值都會收到要求。

要求

  curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        policyTargetKey: {
          targetResource: "orgunits/04fatzly4jbjho9",
        },
        policySchemaFilter: "chrome.users.apps.InstallType"
    }' \
  "https://chromepolicy.googleapis.com/v1/$CUSTOMER/C02l1chq7/policies:resolve"

回應

{
  "resolvedPolicies": [
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.InstallType",
        "value": {
          "appInstallType": "FORCED"
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:fhcfdhnmhdkemdfdncjmgnanbekfmbab"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.InstallType",
        "value": {
          "appInstallType": "ALLOWED"
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    ...
  ]
}

列出機構單位中某應用程式的所有政策

就像您可以在要求中省略應用程式 ID,藉此擷取所有應用程式的政策,您也可以在政策中使用萬用字元,取得單一應用程式的所有政策。在此範例中,我們要取得 Google 繪圖應用程式所有政策的值。

要求

  curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        policyTargetKey: {
          targetResource: "orgunits/04fatzly4jbjho9",
          additionalTargetKeys: {"app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"}
        },
        policySchemaFilter: "chrome.users.apps.*"
    }' \
  "https://chromepolicy.googleapis.com/v1/$CUSTOMER/C02l1chq7/policies:resolve"

回應

{
  "resolvedPolicies": [
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.InstallType",
        "value": {
          "appInstallType": "FORCED"
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.IncludeInChromeWebStoreCollection",
        "value": {
          "includeInCollection": true
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    ...
  ]
}

列出機構單位中所有應用程式的政策

您可以在政策中省略應用程式 ID,並在政策中使用萬用字元,要求機構單位下所有應用程式的所有應用程式設定。

要求

  curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        policyTargetKey: {
          targetResource: "orgunits/04fatzly4jbjho9"
        },
        policySchemaFilter: "chrome.users.apps.*"
    }' \
  "https://chromepolicy.googleapis.com/v1/$CUSTOMER/C02l1chq7/policies:resolve"

回應

{
  "resolvedPolicies": [
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.InstallType",
        "value": {
          "appInstallType": "FORCED"
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:mkaakpdehdafacodkgkpghoibnmamcme"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.IncludeInChromeWebStoreCollection",
        "value": {
          "includeInCollection": true
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:fhcfdhnmhdkemdfdncjmgnanbekfmbab"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.InstallType",
        "value": {
          "appInstallType": "ALLOWED"
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    {
      "targetKey": {
        "targetResource": "orgunits/04fatzly4jbjho9",
        "additionalTargetKeys": {
          "app_id": "chrome:fhcfdhnmhdkemdfdncjmgnanbekfmbab"
        }
      },
      "value": {
        "policySchema": "chrome.users.apps.CertificateManagement",
        "value": {
          "allowAccessToKeys": true
        }
      },
      "sourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      },
      "addedSourceKey": {
        "targetResource": "orgunits/04fatzly4jbjho9"
      }
    },
    ...
  ]
}

正在清除資訊站應用程式設定 AutoLaunchApp

如要清除 AutoLaunchApp,可以將 appId 設為「None」。

請注意,如果將 appId 設為「None」,所有其他 AutoLaunchApp 設定都會設為預設值。在此情況下,系統會忽略要求中傳送的 AppId 以外的所有 AutoLaunchApp 設定。

要求

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
        requests: [{
                policyTargetKey: {
                        targetResource: "orgunits/04fatzly4jbjho9"
                        },
                policyValue: {
                        policySchema: "chrome.devices.kiosk.appsconfig.AutoLaunchApp",
                        value: {appId: "None"}
                        },
                updateMask: {paths: "appId"}
                }]
      }' \
  "https://chromepolicy.googleapis.com/v1/customers/$CUSTOMER/policies/orgunits:batchModify"

回應

成功的回應沒有任何內容。

{}