Sample Resource App


This is a sample Resource App. It uses SAML SSO to identify the user, obtains OAuth credentials and imports content into Schoology.


The following is needed to run this application:

  • The Sample App files
  • A Schoology-assigned OAuth consumer key and consumer secret
  • A web server that can run PHP
  • The Schoology PHP SDK (included in project files)
  • A MySQL database to store the user's OAuth access key

Getting Started

You will need a MySQL database for this project. Enter the database credentials for this application below

define('DB_HOSTNAME', ''); // Database hostname
define('DB_USERNAME', ''); // Database username
define('DB_PASSWORD', ''); // Database password
define('DB_DATABASE', ''); // Database name

Enter your Schoology-assigned consumer key and secret

define('SCHOOLOGY_CONSUMER_KEY', ''); // Your apps schoology consumer key
define('SCHOOLOGY_CONSUMER_SECRET', ''); // Your apps schoology consumer secret

This sample application uses the following database table to store OAuth credentials. Create the schoology_oauth_tokens table using the SQL create statement below

CREATE TABLE  `schoology_oauth_tokens` (
    `uid` int(10) unsigned NOT NULL,
    `token_key` varchar(64) CHARACTER SET utf8 NOT NULL,
    `token_secret` varchar(64) CHARACTER SET utf8 NOT NULL,
    `token_is_access` tinyint(3) unsigned NOT NULL,
    PRIMARY KEY (`uid`)


This Resource App uses SAML SSO to identify the user and obtain OAuth credentials

 * Authentication with SAML 2.0
function schoology_login() {
  if(!$_GET['login']) {
  // Initialize the Schoology class
  // Read the incoming login information.
  $login = $schoology->validateLogin();

  // If the last step failed, then either no information
  // was received or it was invalid.
    // Stop script execution
    print 'No login information was received. Try loading this application again from within Schoology.'; exit;

  // If our session already has a stored ID but it's
  // different from what we received, restart the session.
  if(isset($_SESSION['schoology']['uid']) && $_SESSION['schoology']['uid'] != $login['uid']){
  // The session might already be set if the user is accessing
  // this application again without logging out of Schoology.
  // Only set the session information if not already present
    $_SESSION['schoology'] = $login;
    $_SESSION['session_created'] = time();
  $q_params = parse_url($_REQUEST['RelayState'], PHP_URL_QUERY);
  parse_str($q_params, $app_state);
  $_SESSION['app_state'] = array();
  foreach($app_state as $key => $param){
    $_SESSION['app_state'][$key] = $param;
  header('Location: ' . $_REQUEST['RelayState']); exit;
// Setup our Resource App

// Initialize the session

// Authentication with SAML 2.0

$uid = $_SESSION['schoology']['uid'];
$session_created = $_SESSION['session_created'];
$site_base = ($_SESSION['app_state']['is_ssl'] ? 'https://' : 'http://') . $_SESSION['schoology']['domain'];
$schoology = new SchoologyContentApi(SCHOOLOGY_CONSUMER_KEY, SCHOOLOGY_CONSUMER_SECRET, $site_base);

// Make sure a user is logged in
  print 'No user information was found when loading this page. Please try loading this application from within Schoology.'; exit;

At this point, we should now have a user ID that we can work with. Now that you know who is accessing your application, we can start using the Schoolgy SDK to communicate with the API (eg, import content into Schoology)

Import Content

The following function is responsible for importing content into Schoology (see import workflow).

 * Import content into Schoology
function schoology_import(SchoologyContentApi $schoology) {
  if(!$_GET['import']) {
  $content = schoology_get_sample_content();
  $return_url = '';
  try {
    switch($_GET['import']) {
      case 'link':
        $import_id = $schoology->importLink($content['link']['title'], $content['link']['url']);
        $import_url = $schoology->buildImportUrl($import_id, $return_url);
      case 'embed':
        $import_id = $schoology->importLink($content['embed']['title'], $content['embed']['embed']);
        $import_url = $schoology->buildImportUrl($import_id, $return_url);

      case 'file':
        $import_id = $schoology->importFile($content['file']['filepath']);
        $import_url = $schoology->buildImportUrl($import_id, $return_url);

      case 'bulk':
        $body = array(
          'link' => array(
            array('title' => $content['link']['title'], 'url' => $content['link']['url']),

          'embed' => $embeds = array(
            array('title' => $content['embed']['title'], 'embed' => $content['embed']['embed']),

          'file-attachment' => array('id' => array(
        $result = $schoology->importBulk($body);

        $import_ids = array();
        foreach($result as $i) {
          $import_ids[] = $i->import_id;
        $import_url = $schoology->buildImportUrl($import_ids, $return_url);
  } catch (Exception $e) {
    print $e->getMessage(); exit;
  // Redirct user to schoology import form
  header('Location: ' . $import_url); exit;    

More Examples: Import web link

// Use API to import link and retrieve an import ID
$import_id = $schoology->importLink('Schoology Developers', '');

// Use API utility function @buildImportUrl() to generate the Schoology Import URL
$import_url = $schoology->buildImportUrl($import_id, $return_url);

// Redirct user to Schoology Import Form
header('Location: ' . $import_url); exit;

// After import, Schoology will return the user to the specified $return_url. 
// You can use this $return_url to customize a success message


Congratulations on your first working Resource App! Resource Apps allow users to import content easily from third-party providers. Please refer to the Resource Apps docs for further details.