AnsweredAssumed Answered

API Assignment Comments File Upload

Question asked by David Cutting on Jan 24, 2019
Latest reply on Jan 24, 2019 by David Cutting

Hi there,

 

I want to be able to upload feedback files (from an external marking system) to an assignment much the same way as I can directly on a per-user basis in speed grader.

 

I've queried the API to find various things and am confident at this point that I have a correct canvas ID for the course, the assignment, the user (student) I wish to upload for.

 

When I do the upload via PHP following the docs I get directed to an upload URL and then get back a 201 location which seems to indicate I've succeeded (my code doesn't yet deal with the 301 redirect to confirm upload - I've not seen this on my files to date).

 

However; the file doesn't appear anywhere I can see for the user and requesting the JSON file object passed back in the Location header from the 201 gives me JSON including a "workflow_state" which is "pending_upload".

 

Any pointers please as to what I'm missing? My PHP code (just proof-of-concept so very hacky) is below. Course and assignment etc are stored in the overall class - this is the specific upload file X for ID Y function.

 

Regards,

 

Dave

 

 

function UploadFile($id, $file)
{
echo "Uploading ".$file." for canvas ID ".$id."\n";
$uri = $this->api . "courses/".$this->course."/assignments/".$this->assignment."/submissions/".$id."/comments/files";

$name = substr($file, strrpos($name,"/"));

if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($file);
} else { //
$cFile = '@' . realpath($file);
}

$post = array(
'extra_info' => '123456',
'file_contents'=> $cFile,
'name' => $name,
'size' => filesize($file),
);
$ch = curl_init();
$headers = array(
"Authorization: Bearer ".$this->token,
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL,$uri);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_VERBOSE, true);

$result=curl_exec ($ch);
curl_close ($ch);

$fileupload=json_decode($result,true);

//print_r($fileupload);

$uri = $fileupload['upload_url'];

//echo $uri."\n";
//exit();

$post = array(
'extra_info' => '123456',
'file_contents'=> $cFile,
'name' => $name,
'size' => filesize($file),
);

foreach($fileupload['upload_params'] as $k => $v)
$post[$k]=$v;

$ch = curl_init();
$headers = array(
"Authorization: Bearer ".$this->token,
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL,$uri);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_VERBOSE, true);

$result=curl_exec ($ch);
curl_close ($ch);
}
}

Outcomes