Getting Started with the Media API Using ActionScript

Applies to Roles
Developer
Version
Brightcove 4
Modules
Media API
Edition

This document presents the basic steps to help you get started making Media API queries from ActionScript. This document assumes that you're familiar with the ActionScript programming language, and that you understand the cross-domain security restrictions that Flash imposes on HTTP requests being made in ActionScript. We have a policy file on our servers to enable cross-domain calls. Here are some other docs to review before continuing:

Security best practices. There are a number of important drawbacks and risks to using ActionScript to access the Media API. Chief among them is token security. With access to your token, a hacker gains access to your library of content and in some cases can play your content without your knowledge. In ActionScript your token is compiled into your SWF, but could be extracted by a hacker who decompiles your source code. Read this best practices document on token security for more.

SEO best practices. Making ActionScript requests means you're missing out on any SEO benefits the Media API offers you to get metadata into your pages. Search engine crawlers don't execute Flash applcations. See this best practices document on SEO with the Media API.

Making requests and parsing results

Making HTTP requests from Flash is accomplished most easily using the LoadVars class in ActionScript 2, or the URLLoader class in ActionScript 3. ActionScript will manage the request and handle the response and notify you when results are ready. Here's an AS2 example:

// create request
var url:String = "http://api.brightcove.com/services/library?command=find_all_videos
  &video_fields=id,name,refId&token=BMkaixIhjbhxfa5ATEGTzXm9CrmR8urXRDr9o7bbc64.";
var lv:LoadVars = new LoadVars();
 
lv.onLoad = function (success:Boolean) {
	// handle results
}
 
lv.load(url);

Here's an AS3 example:

var loader:URLLoader;
var url:String = "http://api.brightcove.com/services/library?command=find_all_videos
  &video_fields=id,name,refId&token=BMkaixIhjbhxfa5ATEGTzXm9CrmR8urXRDr9o7bbc64.";
var request:URLRequest = new URLRequest(url);
 
loader = new URLLoader();
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
 
try {
	loader.load(request);
}
catch (error:SecurityError)
{
	trace("A SecurityError has occurred.");
}
 
function errorHandler(evt:Event) {
     // parse results
}
function loaderCompleteHandler(evt:Event) {
	// parse results
}

In both cases, note how we call the Media API method. The form of the read method call is:

<URL>?command=<method_name>&<arguments>=<values>&token=<API_token>

The URL for all Brightcove Media Read API calls is http://api.brightcove.com/services/library. The URL for all Brightcove Media Write API calls is http://api.brightcove.com/services/post. In this example, the method is find_all_videos. In the query parameters, we pass the video_fields argument, which tells the find_all_videos which fields we want in our return object, and the value of our API token.

Parsing the results

ActionScript 3 does not have native support for JSON. A fairly standard way of handling JSON is with the as3core library, which you can download at Google Code. See Accessing the Media API from Flex in AS3 for an example that uses as3corelib.

Tags
ActionScript