จัดการ Cloud Anchor นอกแอป ARCore โดยใช้ ARCore Cloud Anchor Management API
เริ่มต้นใช้งาน
การดำเนินการตัวอย่าง
- แสดงรายการ Cloud Anchor ทั้งหมด
- อัปเดต Time Anchor ของ Cloud Anchor ให้เป็นเวลาสูงสุดที่อนุญาต
- ลบ Cloud Anchor
การให้สิทธิ์
สร้างคีย์บัญชีบริการในคอนโซล Google Cloud Platform และสร้างโทเค็น OAuth2 เพื่อให้สิทธิ์การเรียก Cloud Anchor Management API
ในเมนูการนําทางของคอนโซล Google Cloud Platform ให้ไปที่ APIs & Services > Credentials
เลือกโปรเจ็กต์ที่ต้องการ แล้วคลิก Create Credentials > Service account
ใน Service account details ให้พิมพ์ชื่อสำหรับบัญชีใหม่ จากนั้นคลิก Create
ในหน้า Service account permissions ให้ไปที่เมนูแบบเลื่อนลงของ Select a role เลือก Service Accounts > Service Account Token Creator แล้วคลิก Continue
ในหน้า Grant users access to this service account ให้คลิก Done การดำเนินการนี้จะนำคุณกลับไปที่ APIs & Services > Credentials
ในหน้า Credentials ให้เลื่อนลงไปที่ส่วน Service Accounts และคลิกชื่อบัญชีที่คุณเพิ่งสร้าง
ในหน้า Service account details ให้เลื่อนลงไปที่ส่วน Keys แล้วเลือก Add Key > Create new key
เลือก JSON เป็นประเภทคีย์ แล้วคลิก Create ซึ่งจะดาวน์โหลดไฟล์ JSON ที่มีคีย์ส่วนตัวไปยังเครื่องของคุณ จัดเก็บไฟล์คีย์ JSON ที่ดาวน์โหลดไว้ในตำแหน่งที่ปลอดภัย
สร้างโทเค็น OAuth2
arcore.management
คือขอบเขต OAuth สำหรับ Cloud Anchors Management API โดยค่าเริ่มต้น oauth2l จะทำงานกับแคชโทเค็น คำสั่ง fetch
จะดึงโทเค็นเดียวกัน ใช้ oauth2l เพื่อสร้างโทเค็น OAuth2 สำหรับการให้สิทธิ์ ดังนี้
oauth2l fetch --json creds.json arcore.management
หากต้องการสร้างโทเค็นใหม่ ให้เพิ่มตัวเลือก --cache=""
ในคำสั่ง fetch
oauth2l fetch --cache="" --json creds.json arcore.management
หรือเรียก oauth2l reset
และเรียกใช้คำสั่ง fetch
อีกครั้ง
oauth2l reset
oauth2l fetch --json creds.json arcore.management
แสดงรายการ Cloud Anchor ทั้งหมด
รับหน้าแรกของ Cloud Anchor ซึ่งสามารถจัดเรียงตาม expire_time
, create_time
หรือ last_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
กลับมาจะมีแท็ก Anchor ให้เพิ่มเติมอีก ใช้พารามิเตอร์การค้นหา 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_size
และ order_by
จะต้องสอดคล้องกันในคำขอ page_size
มีค่าเริ่มต้นเป็น 1000
และ order_by
มีค่าเริ่มต้นเป็น
expire_time_desc
อัปเดต Time to Live ของ Cloud Anchor เป็นเวลาสูงสุดที่อนุญาต
ขอ Anchor ของ Cloud 1 รายการเพื่อค้นหา lastLocalizeTime
และ maximumExpireTime
คำขอ:
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"
}
เมื่อมีแท็ก Anchor ให้อัปเดต 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"
}
ลบ Cloud Anchor
ลบ Cloud Anchor รายการเดียว
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" ]}'