ARCore Cloud Anchor Management API

使用 ARCore Cloud Anchor Management API 管理 ARCore 应用之外的云锚点。

开始使用

操作示例

授权

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

列出所有云锚点

获取云锚点的第一页,可选择按 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

将云锚点的存留时间更新为允许的最大时间

请求单个云锚点来查询其 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"

删除一批云锚点:

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" ]}'