Canvas API call to generate a Parent Observer Code

marchermon
Community Explorer

I had code that would make a Canvas API call and return a Parent Observer Code and expiration date to insert into an email I could then send to a parent but it just stopped working. I know my token and address are both correct because they work on all of my other API calls. Any idea? Did the API endpoint get changed or something?

if (isset($_GET['studentID'])) {
$user_id = $_GET['studentID'];
$parents = $_GET['parents'];
$student = $_GET['student'];
$teacher = $_GET['teacher'];
$nameParts = explode(', ', $student);
$student = $nameParts[1] . ' ' . $nameParts[0];

$data = array('user_id' => $user_id);
$url = $canvasAddress.'/api/v1/users/'.$user_id.'/observer_pairing_codes';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $canvasToken,
'Content-Type: application/json',
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if (curl_errno($ch)) {echo 'Error: ' . curl_error($ch);}
curl_close($ch);
$response_data = json_decode($response, true);
// echo "<pre>"; print_r($response_data); echo "</pre>";

$pairingcode = $response_data['code'];
$expires_at = $response_data['expires_at'];


$subject = 'Parent Observer Code for '.$student;
$expires_at = new DateTime($expires_at);
$expires_at = $expires_at->format('l, M. jS \a\t g:i a');
$body = 'Dear Parents of '.$student.':

UCSD uses Canvas as our Learning Management System (LMS). If you would like to monitor your child\'s progress in their courses, you will need to set up a parent observer account in Canvas by using the pairing code below. Please go to bit.ly/CanvasU to learn how to do this. The below pairing code will expire on '.$expires_at.'.

PAIRING CODE: '.$pairingcode.'

Sincerely,
'.$teacher;


// Construct the mailto link with data
$mailtoLink = 'mailto:' . rawurlencode($parents) . '?' .
'subject=' . rawurlencode($subject) . '&' .
'body=' . rawurlencode($body);

echo $mailtoLink;

}

 

0 Likes
Users who also had this question