视频流请求会为直播活动注册新会话。 如需了解详情,请参阅发出直播请求
本页介绍了如何使用 HMAC 令牌对流请求进行身份验证。
准备工作
- 完成前提条件。
- 确保在 Google Ad Manager 账号的直播活动中启用直播创建身份验证密钥。
生成 HMAC 令牌
如需生成令牌,请执行以下操作:
- 收集直播会话请求所需的路径和查询参数。 如需查看完整列表,请参阅方法:stream。
合并参数键和值字符串。您必须按字母顺序对参数进行排序,并使用波浪号
~分隔参数,例如:custom_asset_key=CUSTOM_ASSET_KEY~exp=EXPIRATION~network_code=NETWORK_CODE替换以下内容:
CUSTOM_ASSET_KEY:必需。直播活动的自定义素材资源键。EXPIRATION:必需。令牌的到期时间戳(以 Unix 纪元时间表示)。NETWORK_CODE:必需。您的 Google Ad Manager 账号的广告资源网代码。
使用您的 DAI 身份验证密钥计算令牌字符串的 SHA-256 哈希值。
以十六进制格式设置哈希输出。
如需对令牌字符串进行签名,请将签名附加到之前收集的参数末尾:
custom_asset_key=...~hmac=HMAC_SIGNATURE将
HMAC_SIGNATURE替换为通过使用您的 DAI 身份验证密钥对令牌字符串进行哈希处理而生成的签名。为了安全地传递签名令牌字符串,请对签名令牌字符串应用网址编码。
以下示例对在 60 秒内过期的令牌字符串进行签名和编码:
# 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
使用 HMAC 令牌请求直播会话
如需使用 HMAC 令牌对视频流创建请求进行身份验证,请执行以下操作之一:
HLS
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"
查询字符串参数
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"
表单数据字段
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"
如果成功,您会看到以下 JSON 响应:
{
"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
}
如需了解响应结构和状态代码,请参阅方法:stream。
如果身份验证失败,您会看到以下错误:
<!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
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"
查询字符串参数
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"
表单数据字段
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"
如果成功,您会看到以下 JSON 响应:
{
"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"
}
如需了解响应结构和状态代码,请参阅方法:stream。
如果身份验证失败,您会看到以下错误:
<!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>