ARCore Cloud Anchor Management API

使用 ARCore Cloud Anchor Management API 管理 ARCore 應用程式以外的 Cloud Anchor。

開始使用

範例作業

授權

Google Cloud Platform 主控台中建立服務帳戶金鑰,並產生 OAuth2 憑證,以授權 Cloud Anchor Management API 呼叫。

  1. Google Cloud Platform 主控台的導覽選單中,前往 APIs & Services > Credentials

  2. 選取需要的專案,然後依序點選 Create Credentials > Service account

  3. Service account details 下方輸入新帳戶的名稱,然後按一下 Create

  4. 前往 Service account permissions 頁面的 Select a role 下拉式選單。依序選取 Service Accounts > Service Account Token Creator,然後按一下 Continue

  5. Grant users access to this service account 頁面中,按一下 Done。系統會返回 APIs & Services > Credentials

  6. Credentials 頁面中,向下捲動至 Service Accounts 部分,然後按一下您剛建立的帳戶名稱。

  7. Service account details 頁面中,向下捲動至 Keys 部分,然後選取 Add Key > Create new key

  8. 選取「JSON」做為金鑰類型,然後按一下「Create」。這樣做會將包含私密金鑰的 JSON 檔案下載至您的電腦。將下載的 JSON 金鑰檔案儲存在安全的位置。

產生 OAuth2 權杖

arcore.management 是 Cloud Anchors Management API 的 OAuth 範圍。根據預設,OAuth2l 適用於權杖快取。fetch 指令會擷取相同的權杖。使用 oauth2l 產生 OAuth2 符記:

oauth2l fetch --json creds.json arcore.management

如要產生新權杖,請在 fetch 指令中加入 --cache="" 選項。

oauth2l fetch --cache="" --json creds.json arcore.management

或者,您也可以呼叫 oauth2l reset,然後再次呼叫 fetch 指令。

oauth2l reset
oauth2l fetch --json creds.json arcore.management

列出所有 Cloud 錨點

取得 Cloud Anchors 的第一頁,並可選擇按照 expire_timecreate_timelast_localize_time 排序。

要求:

export BEARER_TOKEN=`(oauth2l fetch --json creds.json arcore.management)`
curl -H "Authorization: Bearer $BEARER_TOKEN" \
"https://arcore.googleapis.com/v1beta2/management/anchors?page_size=50&order_by=last_localize_time%20desc"

回應:

{
  "anchors": [
    {
      "name": "anchors/ua-a1cc84e4f11b1287d289646811bf54d1",
      "createTime": "...",
      "expireTime": "...",
      "lastLocalizeTime": "...",
      "maximumExpireTime": "..."
    },
   …
    {
      "name": "anchors/ua-41a3d0233471917875159f6f3c25ea0e",
      "createTime": "...",
      "expireTime": "...",
      "lastLocalizeTime": "...",
      "maximumExpireTime": "..."
    }
  ],
  nextPageToken: "some-long-string"
}

如果回應傳回 nextPageToken,表示還有其他錨點。在下一個要求中使用 next_page_token 查詢參數,即可擷取下一組結果。

要求:

curl -H "Authorization: Bearer $BEARER_TOKEN" \
"https://arcore.googleapis.com/v1beta2/management/anchors?page_size=50&order_by=last_localize_time%20desc&next_page_token=your-next-page-token-here"

使用 next_page_token 時,所有要求中的 page_sizeorder_by 必須一致。page_size 預設為 1000order_by 則預設為 expire_time_desc

將 Cloud Anchor 的時間更新到允許的最長時間

要求單一 Cloud Anchor 來查詢其 lastLocalizeTimemaximumExpireTime

要求:

export BEARER_TOKEN=`(oauth2l fetch --json creds.json arcore.management)`
curl -H "Authorization: Bearer $BEARER_TOKEN" \
"https://arcore.googleapis.com/v1beta2/management/anchors/your-anchor-id-here"

回應:

{
  "name": "anchors/ua-f21be53fd8ea57f0169c69fbf827f6b7",
  "createTime": "2020-06-29T21:00:00Z",
  "expireTime": "2020-08-28T22:00:00Z",
  "lastLocalizeTime": "2020-06-29T21:00:00Z",
  "maximumExpireTime": "2021-06-29T21:00:00Z"
}

取得錨點後,請將其 expireTime 更新為 maximumExpireTime

要求:

curl -H "Authorization: Bearer $BEARER_TOKEN" -H "Content-Type: application/json" -X "PATCH" \
"https://arcore.googleapis.com/v1beta2/management/anchors/your-anchor-id-here?updateMask=expire_time" \
-d '{ expireTime: "2021-06-29T21:00:00Z" }'

回應:

{
  "name": "anchors/ua-f21be53fd8ea57f0169c69fbf827f6b7",
  "createTime": "2020-06-29T21:00:00Z",
  "expireTime": "2021-06-29T21:00:00Z",
  "lastLocalizeTime": "2020-06-29T21:00:00Z",
  "maximumExpireTime": "2021-06-29T21:00:00Z"
}

刪除雲端錨點

刪除單一雲端錨點:

export BEARER_TOKEN=`(oauth2l fetch --json creds.json arcore.management)`
curl -H "Authorization: Bearer $BEARER_TOKEN" -X "DELETE" \
"https://arcore.googleapis.com/v1beta2/management/anchors/your-anchor-id-here"

刪除一批 Cloud Anchor:

export BEARER_TOKEN=`(oauth2l fetch --json creds.json arcore.management)`
curl -H "Authorization: Bearer $BEARER_TOKEN" -H "Content-Type: application/json" -X "POST" \
"https://arcore.googleapis.com/v1beta2/management/anchors:batchDelete" \
-d '{ names: [ "anchors/your-anchor-id-here", "anchors/your-anchor-id-here" ]}'