Developer API Examples

PHP

Authorization Code Redirect

//We redirect the user to the Authorization Server to authorize access $authorization_url = 'https://access.active911.com/interface/open_api/authorize_agency.php'; //Set the GET parameters and then redirect the user to the authorization url $query_array = array( 'client_id' => '<Your client_id>', 'response_type' => 'code', 'redirection_uri' => '<Your redirection_uri>', 'scope' => '<Scope you want to access>' ); header('Location: ' . $authorization_url . '?' . http_build_query($query_array));

Authorization Code Exchange for Access Token and Refresh Token

//We have been redirected back here after being authorized by the Authorization Server //Now we need to exchange our authorization code for an access token and refresh token $token_url = 'https://access.active911.com/interface/open_api/token.php'; //Note that we send the secret to authenticate our request. We did not send the secret to generate an authorization code because then the user would be able to see it. //We must send the secret from a server or protected native app, where it cannot be grabbed by the user. $post_array = array( 'client_id' => '<Your client_id>', 'client_secret' => '<Your client_secret>', 'grant_type' => 'authorization_code', 'scope' => '<Scope you want to access>', 'code' => $_GET['code'] ); //Now we have the authorization code. We send a request to the token endpoint $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $token_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_VERBOSE, true); $output = curl_exec($ch); $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close ($ch); $json = json_decode($output); //Check if we got a OK response if($response_code != 200){ //There was an error. Show to the user if JSON error was sent if(empty($json->error_description)){ die("Error: Could not generate token"); } else{ die("Error: " . $json->error_description); } } $access_token = $json->access_token; $refresh_token = $json->refresh_token; $expiration = time() + $json->expires_in;

Use Refresh Token to get a new Access Token

//Our access token expired. Request a new one using the refresh token. $token_url = 'https://access.active911.com/interface/open_api/token.php'; //Note that we send the secret again to authenticate our request. $post_array = array( 'client_id' => '<Your client_id>', 'client_secret' => '<Your client_secret>', 'grant_type' => 'refresh_token', 'scope' => '<Scope you want to access>', 'refresh_token' => '<Your previously generated refresh token>' ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $token_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_VERBOSE, true); $output = curl_exec($ch); $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close ($ch); $json = json_decode($output); //Check if we got a OK response if($response_code != 200){ //There was an error. Show to the user if JSON error was sent if(empty($json->error_description)){ die("Error: Could not generate token"); } else{ die("Error: " . $json->error_description); } } //Set the access token and expiration in the cookies $access_token = $json->access_token; $expiration = time() + $json->expires_in;

JavaScript

jQuery AJAX to the API using previously generated access_token

var access_token = "<Your Access Token>"; var base_api_uri = 'https://access.active911.com/interface/open_api/api/'; $.ajax({ type: "GET", url: base_api_uri, beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', "Bearer " + access_token); }, success: function (response) { if(response.result=='success'){ //We successfully fetched the agency's information var name = response.message.agency.name; var latitude = response.message.agency.latitude; var longitude = response.message.agency.longitude; //Store the uri's for all of this agency's devices $( response.message.agency.devices ).each(function(i, device){ devices[device.id] = {uri: device.uri}; }); } else{ alert('Could not connect to the API'); } }, dataType: 'json' });

Java

Authorization Code Redirect w/JavaFX

//We use JavaFX to show the user the Active911 website so that they can authorize the request import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.scene.web.WebEngine; import javafx.scene.web.WebView; import javafx.stage.Stage; public class TestWebView extends Application { private String auth_code; public static void main(String[] args) { Application.launch(args); } @Override public void start(final Stage primaryStage) throws Exception { primaryStage.setTitle("Active911 Connect"); //Lets compose the url //We send the user to the Authorization Server to authorize access String authorization_url = "https://access.active911.com/interface/open_api/authorize_agency.php"; final String redirection_uri = "<Your Redirection URI>"; String client_id = "<Your client id>"; String scope = "<The scope you want to request access to>"; //Set the GET parameters and then send the user to the authorization url authorization_url += "?client_id=" + client_id "&response_type=code" + "&redirection_uri=" + URLEncoder.encode(redirection_uri, "UTF-8") + "&scope=" + scope; //Show the user a browser window with the authorization endpoint WebView browser = new WebView(); WebEngine engine = browser.getEngine(); engine.load(authorization_url); StackPane sp = new StackPane(); sp.getChildren().add(browser); Scene root = new Scene(sp); primaryStage.setScene(root); primaryStage.show(); //Intercept any location that includes the authorization code engine.locationProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> ov, final String oldLoc, final String loc) { System.out.println("Saw location changed to " + loc); //We have been redirected with our authorization code if (loc.contains("code=")) { try { URL url = new URL(loc); String[] params = url.getQuery().split("&"); Map<String, String> map = new HashMap<String, String>(); for (String param : params) { String name = param.split("=")[0]; String value = param.split("=")[1]; map.put(name, value); } System.out.println("The code is: "+map.get("code")); auth_code = map.get("code"); } catch (MalformedURLException e) { e.printStackTrace(); } } } }); } }

GET request to the API using previously generated access_token using the Apache HttpComponents_Client

CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("https://access.active911.com/interface/open_api/api/"); httpget.setHeader("Authorization", "Bearer " + accessToken); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity();

 

Related content