簡介
像布萊特灣平台 API 這樣的 REST API 可以與任何語言一起使用。這裡包含的 Python 腳本只是一個示例,向您展示如何將請求放在一起。還有另一個簡單 GET 請求示例(針對 CMS API)這裡 .
相依性
蟒蛇腳本
下面的代碼顯示了腳本。請注意,要使用它,您需要為以下內容提供自己的值:
- ***此處的帳戶ID ****(第8行)
- ***此處的客戶ID ****(第9行)
- ***這裡的秘密機密****(第10行)
- ***視頻標題在這裡***(第27行)
- ***此處顯示源視頻網址(第40行)
#!/usr/bin/env python
import sys
import requests
import json
import argparse
pub_id = "***ACCOUNT ID HERE****"
client_id = "***CLIENT ID HERE****"
client_secret = "***CLIENT SECRET HERE****"
access_token_url = "https://oauth.brightcove.com/v4/access_token"
profiles_base_url = "https://ingestion.api.brightcove.com/v1/accounts/{pubid}/profiles"
def get_access_token():
access_token = None
r = requests.post(access_token_url, params="grant_type=client_credentials", auth=(client_id, client_secret), verify=False)
if r.status_code == 200:
access_token = r.json().get('access_token')
print(access_token)
return access_token
def create_video():
access_token = get_access_token()
headers = { 'Authorization': 'Bearer ' + access_token, "Content-Type": "application/json" }
url = ("https://cms.api.brightcove.com/v1/accounts/{pubid}/videos/").format(pubid=pub_id)
data = '{"name": "***VIDEO TITLE HERE***"}'
r = requests.post(url, headers=headers, data=data)
return r.json()
def submit_pbi(video_id):
access_token = get_access_token()
print access_token
headers = { 'Authorization': 'Bearer ' + access_token, "Content-Type": "application/json" }
url = ("https://ingest.api.brightcove.com/v1/accounts/{pubid}/videos/{videoid}/ingest-requests").format(pubid=pub_id, videoid=video_id)
print url
data = '''{
"master": { "url": "****SOURCE VIDEO URL HERE***" }
}'''
r = requests.post(url, headers=headers, data=data)
print r.headers
return r.json()
v = create_video()
print v
print submit_pbi(v['id'])