Yêu cầu phát trực tiếp sẽ đăng ký một phiên mới cho sự kiện phát trực tiếp. Để biết thêm thông tin, hãy xem phần Tạo yêu cầu truyền phát trực tiếp
Trang này trình bày cách sử dụng mã thông báo HMAC để xác thực các yêu cầu luồng.
Trước khi bắt đầu
- Hoàn thành các điều kiện tiên quyết.
- Đảm bảo bạn đã bật Khoá xác thực tạo luồng phát trong sự kiện phát trực tiếp trong Tài khoản Google Ad Manager.
Tạo mã thông báo HMAC
Để tạo mã thông báo, hãy làm như sau:
- Thu thập các tham số đường dẫn và truy vấn cần thiết cho yêu cầu phiên phát trực tiếp. Để xem danh sách đầy đủ, hãy xem Phương thức: stream.
Kết hợp các chuỗi khoá và giá trị tham số. Bạn phải sắp xếp các tham số theo bảng chữ cái và phân tách các tham số bằng ký tự dấu ngã
~, ví dụ:custom_asset_key=CUSTOM_ASSET_KEY~exp=EXPIRATION~network_code=NETWORK_CODEThay thế nội dung sau:
CUSTOM_ASSET_KEY: Bắt buộc. Khoá tài sản tuỳ chỉnh của sự kiện phát trực tiếp.EXPIRATION: Bắt buộc. Dấu thời gian hết hạn của mã thông báo theo thời gian bắt đầu của hệ thống Unix.NETWORK_CODE: Bắt buộc. Mã mạng của tài khoản Google Ad Manager.
Tính toán hàm băm SHA-256 của chuỗi mã thông báo bằng khoá xác thực DAI.
Định dạng đầu ra hàm băm ở dạng thập lục phân.
Để ký chuỗi mã thông báo, hãy thêm chữ ký vào cuối các tham số đã thu thập trước đó:
custom_asset_key=...~hmac=HMAC_SIGNATUREThay thế
HMAC_SIGNATUREbằng chữ ký mà bạn đã tạo bằng cách băm chuỗi mã thông báo bằng khoá xác thực DAI.Để truyền chuỗi mã thông báo đã ký một cách an toàn, hãy áp dụng phương thức mã hoá URL cho chuỗi mã thông báo đã ký.
Ví dụ sau đây ký và mã hoá một chuỗi mã thông báo hết hạn trong vòng 60 giây:
# Add 60 seconds to the current time
future_epoch=$((EPOCHSECONDS + 60))
echo "Current: $EPOCHSECONDS"
echo "Future: $future_epoch"
# Current: 1774478306
# Future: 1774478366
# Sample DAI stream authentication key
key="DE0E9..."
# Sort parameters in the token string
token="custom_asset_key=hls-pod-serving-redirect-auth-stream-pod~exp=1774478366~network_code=21775744923"
# Generate the token's signature.
echo -n $token | openssl dgst -sha256 -mac HMAC -macopt key:$key
# SHA2-256(stdin)= 17cdf7079b735320dbc66e4c9d677ae0380fb0ef3cf9ce90fdd55d0667574365
# Sign the token: custom_asset_key=hls-pod-serving-redirect-auth-stream-pod~exp=1774478366~network_code=21775744923~hmac=17cdf7079b735320dbc66e4c9d677ae0380fb0ef3cf9ce90fdd55d0667574365
# Encode the token: custom_asset_key%3Dhls-pod-serving-redirect-auth-stream-podexp%3D1774478366network_code%3D21775744923~hmac%3D17cdf7079b735320dbc66e4c9d677ae0380fb0ef3cf9ce90fdd55d0667574365
Sử dụng mã thông báo HMAC để yêu cầu một phiên phát trực tiếp
Để xác thực yêu cầu tạo luồng bằng mã thông báo HMAC, hãy làm theo một trong những cách sau:
HLS (Phát trực tuyến dựa trên HTTP)
Tiêu đề của yêu cầu Authorization
curl "https://dai.google.com/ssai/pods/api/v1/network/21775744923/custom_asset/hls-pod-serving-redirect-auth-stream-pod/stream" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: DCLKDAI token=custom_asset_key%3Dhls-pod-serving-redirect-auth-stream-podexp%3D1774478366network_code%3D21775744923~hmac%3D17cdf7079b735320dbc66e4c9d677ae0380fb0ef3cf9ce90fdd55d0667574365"
Tham số chuỗi truy vấn
curl "https://dai.google.com/ssai/pods/api/v1/network/21775744923/custom_asset/hls-pod-serving-redirect-auth-stream-pod/stream?auth-token=custom_asset_key%3Dhls-pod-serving-redirect-auth-stream-podexp%3D1774478366network_code%3D21775744923~hmac%3D17cdf7079b735320dbc66e4c9d677ae0380fb0ef3cf9ce90fdd55d0667574365" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded"
Trường dữ liệu biểu mẫu
curl "https://dai.google.com/ssai/pods/api/v1/network/21775744923/custom_asset/hls-pod-serving-redirect-auth-stream-pod/stream" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "auth-token=custom_asset_key%3Dhls-pod-serving-redirect-auth-stream-podexp%3D1774478366network_code%3D21775744923~hmac%3D17cdf7079b735320dbc66e4c9d677ae0380fb0ef3cf9ce90fdd55d0667574365"
Nếu thành công, bạn sẽ thấy phản hồi JSON sau:
{
"stream_id":"6299a01b-450d-4879-a5c8-fe3cf2188aee:CBF2",
"media_verification_url":"https://dai.google.com/view/p/service/linear/stream/6299a01b-450d-4879-a5c8-fe3cf2188aee:CBF2/loc/CBF2/network/21775744923/event/C5BT3czhT2Sc7OIbM8ibqA/media/","metadata_url":"https://dai.google.com/linear/pods/hls/pa/event/C5BT3czhT2Sc7OIbM8ibqA/stream/6299a01b-450d-4879-a5c8-fe3cf2188aee:CBF2/metadata",
"session_update_url":"https://dai.google.com/linear/v1/pa/event/C5BT3czhT2Sc7OIbM8ibqA/stream/6299a01b-450d-4879-a5c8-fe3cf2188aee:CBF2/session",
"polling_frequency":10
}
Để hiểu cấu trúc phản hồi và mã trạng thái, hãy tham khảo Phương thức: stream.
Nếu xác thực không thành công, bạn sẽ thấy lỗi sau:
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 401 (Unauthorized)!!1</title>
...
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>401.</b> <ins>That’s an error.</ins>
<p><ins>That’s all we know.</ins>s
DASH (Truyền phát thích ứng động qua HTTP)
Tiêu đề của yêu cầu Authorization
curl "https://dai.google.com/ssai/pods/api/v1/network/21775744923/custom_asset/dash-pod-serving-redirect-auth-stream-pod/stream"\
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: DCLKDAI token=custom_asset_key%3Ddash-pod-serving-redirect-auth-stream-podexp%3D1772817105network_code%3D21775744923~hmac%3Dc7e10f51f544610cdda28fdee903472839bf1154701ae371c0196d8f63c8bdda"
Tham số chuỗi truy vấn
curl "https://dai.google.com/ssai/pods/api/v1/network/21775744923/custom_asset/dash-pod-serving-redirect-auth-stream-pod/stream?auth-token=custom_asset_key%3Ddash-pod-serving-redirect-auth-stream-podexp%3D1772817105network_code%3D21775744923~hmac%3Dc7e10f51f544610cdda28fdee903472839bf1154701ae371c0196d8f63c8bdda" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded"
Trường dữ liệu biểu mẫu
curl "https://dai.google.com/ssai/pods/api/v1/network/21775744923/custom_asset/dash-pod-serving-redirect-auth-stream-pod/stream" \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "auth-token=custom_asset_key%3Ddash-pod-serving-redirect-auth-stream-podexp%3D1772817105network_code%3D21775744923~hmac%3Dc7e10f51f544610cdda28fdee903472839bf1154701ae371c0196d8f63c8bdda"
Nếu thành công, bạn sẽ thấy phản hồi JSON sau:
{
"stream_id": "e66568dc-cb5c-4859-b645-e12d9b5b821b:ATL",
"media_verification_url": "https://dai.google.com/view/p/service/linear/stream/e66568dc-cb5c-4859-b645-e12d9b5b821b:ATL/loc/ATL/network/21775744923/event/YMTFNxBxTR66kFv-krZHcQ/media/",
"metadata_url": "https://dai.google.com/linear/pods/dash/pa/event/YMTFNxBxTR66kFv-krZHcQ/stream/e66568dc-cb5c-4859-b645-e12d9b5b821b:ATL/metadata",
"session_update_url": "https://dai.google.com/linear/v1/pa/event/YMTFNxBxTR66kFv-krZHcQ/stream/e66568dc-cb5c-4859-b645-e12d9b5b821b:ATL/session",
"polling_frequency": 10,
"pod_manifest_url": "https://dai.google.com/linear/pods/v1/dash/event/YMTFNxBxTR66kFv-krZHcQ/stream/e66568dc-cb5c-4859-b645-e12d9b5b821b:ATL/pod/$pod-id$/manifest.mpd",
"manifest_format": "dash"
}
Để hiểu cấu trúc phản hồi và mã trạng thái, hãy tham khảo Phương thức: stream.
Nếu xác thực không thành công, bạn sẽ thấy lỗi sau:
<!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
<title>Error 401 (Unauthorized)!!1</title>
...
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>401.</b> <ins>That’s an error.</ins>
<p><ins>That’s all we know.</ins>