Đối với tính năng chèn quảng cáo do máy chủ hướng dẫn, ứng dụng của bạn sẽ đưa ra yêu cầu về tệp kê khai nhóm quảng cáo. Để biết thêm thông tin, hãy xem phần Tạo URL tệp kê khai nhóm quảng cáo.
Trang này trình bày cách xác thực các yêu cầu về tệp kê khai pod bằng mã thông báo Mã xác thực thông báo dựa trên hàm băm (HMAC).
Trước khi bắt đầu
Trước khi tiếp tục, hãy làm như sau:
- 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ài nguyên Pod 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 đường dẫn và các tham số truy vấn (ngoại trừ mã thông báo xác thực) được điền sẵn cho yêu cầu tệp kê khai nhóm. Để xem danh sách đầy đủ, hãy xem Phương thức: Tệp kê khai nhóm HLS hoặc Phương thức: Tệp kê khai nhóm DASH.
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ụ:ad_break_id=AD_BREAK_ID~custom_asset_key=CUSTOM_ASSET_KEY~exp=EXPIRATION~network_code=NETWORK_CODE~pd=POD_DURATIONTí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ố mà bạn đã thu thập trước đó:
ad_break_id=...~hmac=HMAC_SIGNATURE`Thay 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 tạo ra giá trị được mã hoá URL của một chuỗi mã thông báo đã ký:
HLS (Phát trực tuyến dựa trên HTTP)
# Add 60 seconds to the current time
future_epoch=$((EPOCHSECONDS + 60))
echo "Current: $EPOCHSECONDS"
echo "Future: $future_epoch"
# Current: 1774464277
# Future: 1774464337
# Sample DAI pod resource authentication key
key="EB089..."
# Sort parameters in the token string
token="ad_break_id=ab-001~custom_asset_key=hls-pod-serving-manifest-auth-stream-pod~exp=1774464337~network_code=21775744923~pd=30000"
# Generate the token's signature.
echo -n $token | openssl dgst -sha256 -mac HMAC -macopt key:$key
# SHA2-256(stdin)= 68ce65522e0b5f0b4e9a842a0a300e5cba8d14502268b18f66619abcc4dc016e
# Sign the token: ad_break_id=ab-001~custom_asset_key=hls-pod-serving-manifest-auth-stream-pod~exp=1774464337~network_code=21775744923~pd=30000~hmac=68ce65522e0b5f0b4e9a842a0a300e5cba8d14502268b18f66619abcc4dc016e
# Encode the token:
ad_break_id%3Dab-001~custom_asset_key%3Dhls-pod-serving-manifest-auth-stream-pod~exp%3D1774464337~network_code%3D21775744923~pd%3D30000~hmac%3D68ce65522e0b5f0b4e9a842a0a300e5cba8d14502268b18f66619abcc4dc016e
DASH (Truyền phát thích ứng động qua HTTP)
# Add 60 seconds to the current time
future_epoch=$((EPOCHSECONDS + 60))
echo "Current: $EPOCHSECONDS"
echo "Future: $future_epoch"
# Current: 1774464770
# Future: 1774464830
# Sample DAI pod resource authentication key
key="EB08..."
# Assemble parameters in the token string
token="ad_break_id=ab-001~custom_asset_key=dash-pod-serving-manifest-auth-stream-pod~exp=1774464830~network_code=21775744923~pd=30000"
# Generate the token's signature.
echo -n $token | openssl dgst -sha256 -mac HMAC -macopt key:$key
# SHA2-256(stdin)= 51ce9544b2f04a27d5a818bd982c262f97b7bc64d9e8ea07d13ae73e04062614
# Sign the token: ad_break_id=ab-001~custom_asset_key=dash-pod-serving-manifest-auth-stream-pod~exp=1774464830~network_code=21775744923~pd=30000~hmac=51ce9544b2f04a27d5a818bd982c262f97b7bc64d9e8ea07d13ae73e04062614
# Encode the token: ad_break_id%3Dab-001~custom_asset_key%3Ddash-pod-serving-manifest-auth-stream-pod~exp%3D1774464830~network_code%3D21775744923~pd%3D30000~hmac%3D51ce9544b2f04a27d5a818bd982c262f97b7bc64d9e8ea07d13ae73e04062614
Xác thực yêu cầu tệp kê khai nhóm
Để xác thực các yêu cầu tệp kê khai nhóm, hãy sử dụng tham số chuỗi truy vấn auth-token để truyền mã thông báo HMAC đã ký được mã hoá URL.
HLS (Phát trực tuyến dựa trên HTTP)
Ví dụ sau đây sử dụng mã thông báo HMAC để xác thực yêu cầu về tệp kê khai nhóm HLS:
curl --include "https://dai.google.com/linear/pods/v1/hls/network/21775744923/custom_asset/hls-pod-serving-manifest-auth-stream-pod/ad_break_id/ab-001.m3u8?stream_id=381c29ff-9015-4f9f-8a43-e2e13822473a:ATL&pd=30000&auth-token=ad_break_id%3Dab-001~custom_asset_key%3Dhls-pod-serving-manifest-auth-stream-pod~exp%3D1774464337~network_code%3D21775744923~pd%3D30000~hmac%3D68ce65522e0b5f0b4e9a842a0a300e5cba8d14502268b18f66619abcc4dc016e"
Nếu thành công, bạn sẽ thấy phản hồi sau:
...
< HTTP/2 200
< vary: Accept-Encoding
<
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA:TYPE=AUDIO,URI="https://dai.google.com/linear/pods/v1/hls/event/o35L8Xl8TFa2naph5beXsw/ab_break_id/ab-001/profile/fmp4-audio-128kbps.m3u8?pd=30000&stream_id=9331d770-ae82-460a-96d4-b971eb6f8fa0%3ADLS",GROUP-ID="AUDIO",NAME="mp4a.40.2-0",DEFAULT=YES
#EXT-X-STREAM-INF:BANDWIDTH=1268994,RESOLUTION=1280x720,CODECS="avc1.64001e,mp4a.40.2",AUDIO="AUDIO"
https://dai.google.com/linear/pods/v1/hls/event/o35L8Xl8TFa2naph5beXsw/ad_break_id/ab-001/profile/Video-1200k.m3u8?pd=30000&stream_id=9331d770-ae82-460a-96d4-b971eb6f8fa0%3ADLS
#EXT-X-STREAM-INF:BANDWIDTH=5114591,RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2",AUDIO="AUDIO"
...
Để 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: Tệp kê khai nhóm HLS.
Nếu xác thực không thành công, bạn sẽ thấy biểu tượng x-ad-manager-dai-warning:
...
< HTTP/2 200
< x-ad-manager-dai-warning: Unable to create ad break due to Unauthorized error (skipping ad break creation)
....
<
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA:TYPE=AUDIO,URI="https://dai.google.com/linear/pods/v1/hls/event/o35L8Xl8TFa2naph5beXsw/ad_break_id/ab-001/profile/fmp4-audio-128kbps.m3u8?pd=30000&stream_id=381c29ff-9015-4f9f-8a43-e2e13822473a%3AATL",GROUP-ID="AUDIO",NAME="mp4a.40.2-0",DEFAULT=YES
#EXT-X-STREAM-INF:BANDWIDTH=1268994,RESOLUTION=1280x720,CODECS="avc1.64001e,mp4a.40.2",AUDIO="AUDIO"
https://dai.google.com/linear/pods/v1/hls/event/o35L8Xl8TFa2naph5beXsw/ad_break_id/ab-001/profile/Video-1200k.m3u8?pd=30000&stream_id=381c29ff-9015-4f9f-8a43-e2e13822473a%3AATL
#EXT-X-STREAM-INF:BANDWIDTH=5114591,RESOLUTION=1280x720,CODECS="avc1.4d401f,mp4a.40.2",AUDIO="AUDIO"
https://dai.google.com/linear/pods/v1/hls/event/o35L8Xl8TFa2naph5beXsw/ad_break_id/ab-001/profile/media-ts-4628000bps.m3u8?pd=30000&stream_id=381c29ff-9015-4f9f-8a43-e2e13822473a%3AATL
DASH (Truyền phát thích ứng động qua HTTP)
Ví dụ sau đây sử dụng mã thông báo HMAC để xác thực yêu cầu tệp kê khai pod DASH:
curl --include "https://dai.google.com/linear/pods/v1/dash/network/21775744923/custom_asset/dash-pod-serving-manifest-auth-stream-pod/stream/310b1882-4a62-436a-99b1-ca56435b48f6:TUL/ad_break_id/ab-001/manifest.mpd?pd=30000&auth-token=ad_break_id%3Dab-001~custom_asset_key%3Ddash-pod-serving-manifest-auth-stream-pod~exp%3D1774464830~network_code%3D21775744923~pd%3D30000~hmac%3D51ce9544b2f04a27d5a818bd982c262f97b7bc64d9e8ea07d13ae73e04062614"
Nếu thành công, bạn sẽ thấy phản hồi sau:
...
HTTP/2 200
<?xml version="1.0" encoding="UTF-8"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:_XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" _XMLSchema-instance:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" id="201" profiles="urn:hbbtv:dash:profile:isoff-live:2012" type="static" mediaPresentationDuration="PT30S" minBufferTime="PT2S">
<Period id="1" duration="PT30S">
<BaseURL>https://dai.google.com/linear/pods/v1/seg/event/310b1882-4a62-436a-99b1-ca56435b48f6:TUL/ad_break_id/ab-001/profile/</BaseURL>
<SegmentTemplate startNumber="0" media="$RepresentationID$/$Number$.mp4?stream_id=310b1882-4a62-436a-99b1-ca56435b48f6:TUL&sd=5000&pd=30000" initialization="$RepresentationID$/init.mp4?stream_id=310b1882-4a62-436a-99b1-ca56435b48f6:TUL&pd=30000">
<SegmentTimeline>
<S d="5" r="5"/>
</SegmentTimeline>
</SegmentTemplate>
<AdaptationSet mimeType="video/mp4" scanType="progressive" contentType="video">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="main"/>
<Representation width="1280" height="720" frameRate="25" codecs="avc1.64001f" id="Video-1200k" bandwidth="1300000">
<InbandEventStream schemeIdUri="https://developer.apple.com/streaming/emsg-id3"/>
</Representation>
</AdaptationSet>
<AdaptationSet mimeType="audio/mp4" scanType="progressive" contentType="audio">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="main"/>
<Representation audioSamplingRate="48000" codecs="mp4a.40.2" id="fmp4-audio-128kbps" bandwidth="128000">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
</Representation>
</AdaptationSet>
</Period>
</MPD>
Để 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: Tệp kê khai pod DASH.
Nếu xác thực không thành công, bạn sẽ thấy biểu tượng x-ad-manager-dai-warning:
...
< HTTP/2 200
< access-control-allow-headers: Authorization
< x-ad-manager-dai-warning: Unable to create ad break due to Unauthorized error (skipping ad break creation)
<
<?xml version="1.0" encoding="UTF-8"?>
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" xmlns:_XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" _XMLSchema-instance:schemaLocation="urn:mpeg:dash:schema:mpd:2011 DASH-MPD.xsd" id="201" profiles="urn:hbbtv:dash:profile:isoff-live:2012" type="static" mediaPresentationDuration="PT30S" minBufferTime="PT2S">
<Period id="1" duration="PT30S">
<BaseURL>https://dai.google.com/linear/pods/v1/seg/event/310b1882-4a62-436a-99b1-ca56435b48f6:TUL/ad_break_id/ab-001/profile/</BaseURL>
<SegmentTemplate startNumber="0" media="$RepresentationID$/$Number$.mp4?stream_id=310b1882-4a62-436a-99b1-ca56435b48f6:TUL&sd=5000&pd=30000" initialization="$RepresentationID$/init.mp4?stream_id=310b1882-4a62-436a-99b1-ca56435b48f6:TUL&pd=30000">
<SegmentTimeline>
<S d="5" r="5"/>
</SegmentTimeline>
</SegmentTemplate>
<AdaptationSet mimeType="video/mp4" scanType="progressive" contentType="video">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="main"/>
<Representation width="1280" height="720" frameRate="25" codecs="avc1.64001f" id="Video-1200k" bandwidth="1300000">
<InbandEventStream schemeIdUri="https://developer.apple.com/streaming/emsg-id3"/>
</Representation>
</AdaptationSet>
<AdaptationSet mimeType="audio/mp4" scanType="progressive" contentType="audio">
<Role schemeIdUri="urn:mpeg:dash:role:2011" value="main"/>
<Representation audioSamplingRate="48000" codecs="mp4a.40.2" id="fmp4-audio-128kbps" bandwidth="128000">
<AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
</Representation>
</AdaptationSet>
</Period>
</MPD>