Found this content helpful? Log in or sign up to leave a like!

My custom javascript cannot pull the Canvas User ID while used in Canvas, but can in SCORM cloud

Jump to solution
CindyFenske
Community Novice

Hello! 

In my Storyline project, we are hosting an artificial intelligence based tutor on an AWS server - to keep some privacy, the start of the URL is https://s3.us-east-2.amazonaws.com and the end is /index.html?user=[%UserID%]

Our goal is to have the following javascript search for the Canvas User ID, store it in the URL, and then keep track of that user's conversations with the AI tutor: 

 

 

// Get the SCORM API
function getAPI(win) {
    var API = null;
    while (win && !API && win.parent && win.parent !== win) {
        win = win.parent;
        API = win.GetPlayer ? win.GetPlayer() : null;
    }
    return API;
}

// Retrieve Canvas User ID
var API = getAPI(window);
var userID = "unknown"; // Default value

if (API && API.GetStudentID) {
    userID = API.GetStudentID();
    
    // Debugging: Show the User ID if it's retrieved
    alert("User ID Retrieved: " + userID);
} else {
    // Debugging: Show an error message if User ID is not retrieved
    alert("Error: User ID not retrieved. SCORM API might not be available.");
}

// Set the User ID in Storyline Variable
var player = GetPlayer();
player.SetVar("UserID", userID);

// Confirm that the variable has been set (optional)
alert("User ID set in Storyline: " + userID);

 

 

 

When the user clicks the red button, it runs this script and also opens the link to our AI tutor. (See screenshot 1). When we export the storyline as a SCORM package and import it into SCORM cloud, the javascript successfully searches for the UserID and tries to populate it in both the URL and the (temporary for testing) "User ID:" field in Storyline, but because it can't find it, it displays error messages. (see screenshot 2). 

However, when we upload this same file to Canvas, the url just keeps it's placeholder and does not display any error messages. 

The inspector in Chrome shows screenshot 3, but we confirmed our cross-origin frame permissions are wide-open and should not be causing any problems. 

So we're wondering if something on Canvas's end is blocking our ability to gather the user ID. I'm happy to detail the situation more, but is anyone able to help? I'm just looking for a solution that finds the user ID in Canvas and puts it into the AWS url. We have a tight turnaround on this and either hope it's a solvable issue or the issue isn't on our end. 

Thank you in advance! 

Labels (1)
0 Likes
1 Solution
bressler1995
Community Member

For those stumbling on this post like I did when trying to figure out a way to reliably do this on the web version and the mobile app, I figured it out.  I learned before that students can make API calls only to things they have access to without needing a token (i.e, their own user details) as everything in Canvas is loaded via API.  I can't speak to doing it in scorm but this is the js solution.

const initProcedure = () => {
        // API docs indicate you can use 'self' instead of a harcoded ID
        fetch("https://yourorg.instructure.com/api/v1/users/self/profile", {
            method: 'GET',
            headers: {
                'Accept': 'application/json'
            }
        })
        .then(response => {
            console.log(response)
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
                    
            return response.json();
        })
        .then(data => {
            if(data != null) {
                const uid = data.id;
                // This is the user's ID, do something useful with it.
                console.log(uid);
            }
        })
        .catch(error => {
            console.error(error);
        });
    };

 

View solution in original post

0 Likes