Tutorial: play your first video¶
In this tutorial you do the full path of a partner integration with your test credentials: you record a subscription for a test user, authorize the user, get the stream of a video and play it in a browser. When you complete it, all parts of your integration are known to work.
Time: about 30 minutes.
Before you start¶
Make sure that you have:
- The test credentials from MEGOGO: the test partner ID, the public key and the private key of one platform, the salt and the service ID of the test subscription. See Credentials.
- The IP address of your computer in the IP list of the Billing API. See Credentials.
- Python 3 and a browser.
- You did Getting started: your signature is correct.
All requests go to the production servers. The test user and the test subscription are real records of your test partner account. Step 6 removes the subscription.
Step 1. Prepare the script¶
Save this script as megogo_tutorial.py. Put your test values in the
first lines.
import json
import urllib.parse
import urllib.request
from hashlib import md5
PARTNER_ID = "{partner_id}" # test partner ID, for example testprod_ua
PUBLIC_KEY = "{public_key}"
PRIVATE_KEY = "{private_key}"
SALT = "{salt}"
SERVICE_ID = "{service_id}" # the test subscription
USER_ID = "tutorial-user-001" # any new user identifier
API = "https://api.megogo.net/v1/"
BILLING = f"https://billing.megogo.net/partners/{PARTNER_ID}/"
def content_api(path, **params):
"""Send a signed Content API request. The order of the parameters is kept."""
joined = "".join(f"{k}={v}" for k, v in params.items())
sign = md5((joined + PRIVATE_KEY).encode("utf-8")).hexdigest() + PUBLIC_KEY
query = urllib.parse.urlencode({**params, "sign": sign})
with urllib.request.urlopen(f"{API}{path}?{query}") as response:
return json.load(response)
def billing_api(path, **params):
"""Send a Billing API request. It is not signed: MEGOGO checks your IP address."""
query = urllib.parse.urlencode(params)
with urllib.request.urlopen(f"{BILLING}{path}?{query}") as response:
return json.load(response)
Step 2. Record a subscription for a test user¶
Add these lines to the script and run it:
print(billing_api("subscription/subscribe", userId=USER_ID, serviceId=SERVICE_ID))
print(billing_api("user/subscriptions", identifier=USER_ID))
Expected result:
{'successful': True}
{'identifier': 'tutorial-user-001', 'svod': [{'serviceId': '...', 'serviceName': '...', 'dateFinish': ..., 'dateFinishString': '...'}]}
MEGOGO created the user and recorded the subscription. In production, you send this request after the user pays in your billing. See Sell a subscription.
Step 3. Get an access token for the user¶
The authorization token is the MD5 hash of the user identifier, the partner ID and the salt:
token = md5((USER_ID + PARTNER_ID + SALT).encode("utf-8")).hexdigest()
auth = content_api("auth/by_partners", isdn=USER_ID, partner_key=PARTNER_ID, token=token)
access_token = auth["data"]["tokens"]["access_token"]
print(auth["result"], access_token[:12] + "...")
Expected result: ok and the start of the access token. See
Authorize a user.
Step 4. Find a video that the user can watch¶
Take the ID of a film with vod="svod" from your XML feed, or a film from
the test data. Then make sure that the user
can watch it:
VIDEO_ID = 6680 # replace with a video of your feed
info = content_api("video/info", id=VIDEO_ID, lang="en", access_token=access_token)
print(info["data"]["title"], info["data"]["is_available"], info["data"]["delivery_rules"])
Expected result: the title, True and ['svod']. If you get False,
restrictions in the response tells why. Take another video.
Step 5. Get the stream and play it¶
Request the stream with the access token of the user:
stream = content_api("stream", video_id=VIDEO_ID, lang="en", access_token=access_token)
data = stream["data"]
print(data["stream_type"], repr(data["drm_type"]))
print(data["src"])
Expected result: hls '' (HLS without DRM) and the URL of the manifest.
If src is empty, read restrictions. See
Play a video.
To play the stream, save this page as player.html, put the src value
in it and open it in a browser:
<!doctype html>
<video id="video" controls width="640"></video>
<script src="https://cdn.jsdelivr.net/npm/hls.js@1.5.17/dist/hls.min.js"></script>
<script>
const src = "PUT_THE_SRC_VALUE_HERE";
const video = document.getElementById("video");
if (video.canPlayType("application/vnd.apple.mpegurl")) {
video.src = src; // Safari plays HLS without a library
} else {
const hls = new Hls(); // other browsers: hls.js
hls.loadSource(src);
hls.attachMedia(video);
}
</script>
The video plays. If your key gives DASH or DRM (stream_type is dash,
or drm_type is not empty), this simple page cannot play it. See
Play a video and DRM.
WARNING
The src URL is for this user and this playback only. Do not save it
or send it to other users. Request a new src each time the user
starts the playback.
Step 6. Remove the test subscription¶
print(billing_api("subscription/unsubscribe", userId=USER_ID, serviceId=SERVICE_ID))
print(billing_api("user/subscriptions", identifier=USER_ID))
Expected result: {'successful': True}, then 'svod': [].
What you did¶
| Step | Interface | What happened |
|---|---|---|
| 2 | Billing API | MEGOGO recorded a subscription for your user |
| 3 | Content API | Your server got an access token for the user |
| 4 | Content API | You found a video that the user can watch |
| 5 | Content API, CDN | Your player played the stream |
| 6 | Billing API | You stopped the subscription |
Next steps¶
- Build and update the catalog from the XML feed.
- Play a video with DASH and DRM.
- Go-live checklist before you open the service to your users.
Guide version 1.4.1, 2026-10-31 · Report a problem on this page