Community

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
sutaria
New Member

Please help with a .Net API call

Jump to solution

I am trying to upload a courses file using the .Net API. I have borrowed most of the code from  @garth ‌'s example, which was very helpful btw. 

I am able to hit the Canvas site, I can see the token getting used but I am unable to get past that. I get a very unfriendly error message - "Error while importing CSV. Please contact support (Error report xxxxx)"

I have a CSV with UTF-8 Encoding. If I manually upload the file from within Canvas, it works. This tells me that the file is fine.

I checked with Canvas folks and they reported the error they are seeing is about "unknown mime type text/plain".

Attaching a snapshot of the code I am using. Can somebody look at it and see if you can find something obviously wrong? Any pointers will be highly appreciated. Let me know if you need more details.

276586_Canvas API code.JPG

1 Solution

Accepted Solutions

Here ya go! I think this is a modification of something they had on their old unsupported Github but it still works for us.

$sourceDir = "C:\data\" #this is source directory literal path
$outputPath = "C:\data\output\" #output path for the zip file creation
$domain = "yourdomain.instructure.com"
$account_id = "1"
$token = "TOKEN GOES HERE" # access_token
$outputZip = "imports.csv.zip" # name of the zip file to create

#################################################
###### Don't edit anything after this line ######
#################################################
$url = "https://$domain/api/v1/accounts/"+$account_id+"/sis_imports.json?import_type=instructure_csv"
$headers = @{"Authorization"="Bearer "+$token}

# Just in case $sourceDir doesn't end with a \, add it.
if(!($sourceDir.EndsWith('\'))){
    $sourceDir += "\"
    Write-Host "You sourceDir didn't end with a \ so I added one.  It really is important"
}
if($outputZip.Contains('\')){
    Write-Host "The outputZip should not contain backslashes.  You are warned"
}

###### Some functions

$contentType = "application/zip" # don't change
$InFile = $outputPath+$outputZip # don't change
write-zip -Path $sourceDir"*.csv" -OutputPath $InFile

$t = get-date -format M_d_y_h
$status_log_path = $outputPath+$t+"nonenrollments-status.log"

$results1 = (Invoke-WebRequest -Headers $headers -InFile $InFile -Method POST -ContentType $contentType -Uri $url) #-PassThru -OutFile $outputPath$t"-status.log"
$results1.Content | Out-File $status_log_path
$results = ($results1.Content | ConvertFrom-Json)

do{
  Write-Host $status_line
  $status_url = "https://$domain/api/v1/accounts/"+$account_id+"/sis_imports/"+$results.id
  $results1 = (Invoke-WebRequest -Headers $headers -Method GET -Uri $status_url) #-PassThru -OutFile $outputPath$t"-status.log"
  $results1.Content | Out-File -Append $status_log_path
  $results = ($results1.Content | ConvertFrom-Json)
  Start-Sleep -s 300

if($results -eq $null){
    break
  }
}
while($results.progress -lt 100 -and $results.workflow_state -ne "failed_with_messages")
$results1.Content | Out-File -Append $status_log_path

# The sis import is done, you might do something else here like trigger course copies

Move-Item -Force $outputPath$outputZip $outputPath$t-$outputZip
Remove-Item $sourceDir*.csv

View solution in original post

6 Replies
MattHanes
Community Member

Ok, so I'm not a programmer but I am going to attempt to help anyway.

Try zipping the file before you send it and hard-code the content-type to be "application/zip".

 @MattHanes ‌ Thank you for the reply. I did what you suggested and still get same error Smiley Sad
Any other pointers that you can think of?

I can't think of anything else. We use Powershell to send our uploads every night. Would it help if I shared that with you?

Yes please....that would be great. Thank you very much for the help.

Here ya go! I think this is a modification of something they had on their old unsupported Github but it still works for us.

$sourceDir = "C:\data\" #this is source directory literal path
$outputPath = "C:\data\output\" #output path for the zip file creation
$domain = "yourdomain.instructure.com"
$account_id = "1"
$token = "TOKEN GOES HERE" # access_token
$outputZip = "imports.csv.zip" # name of the zip file to create

#################################################
###### Don't edit anything after this line ######
#################################################
$url = "https://$domain/api/v1/accounts/"+$account_id+"/sis_imports.json?import_type=instructure_csv"
$headers = @{"Authorization"="Bearer "+$token}

# Just in case $sourceDir doesn't end with a \, add it.
if(!($sourceDir.EndsWith('\'))){
    $sourceDir += "\"
    Write-Host "You sourceDir didn't end with a \ so I added one.  It really is important"
}
if($outputZip.Contains('\')){
    Write-Host "The outputZip should not contain backslashes.  You are warned"
}

###### Some functions

$contentType = "application/zip" # don't change
$InFile = $outputPath+$outputZip # don't change
write-zip -Path $sourceDir"*.csv" -OutputPath $InFile

$t = get-date -format M_d_y_h
$status_log_path = $outputPath+$t+"nonenrollments-status.log"

$results1 = (Invoke-WebRequest -Headers $headers -InFile $InFile -Method POST -ContentType $contentType -Uri $url) #-PassThru -OutFile $outputPath$t"-status.log"
$results1.Content | Out-File $status_log_path
$results = ($results1.Content | ConvertFrom-Json)

do{
  Write-Host $status_line
  $status_url = "https://$domain/api/v1/accounts/"+$account_id+"/sis_imports/"+$results.id
  $results1 = (Invoke-WebRequest -Headers $headers -Method GET -Uri $status_url) #-PassThru -OutFile $outputPath$t"-status.log"
  $results1.Content | Out-File -Append $status_log_path
  $results = ($results1.Content | ConvertFrom-Json)
  Start-Sleep -s 300

if($results -eq $null){
    break
  }
}
while($results.progress -lt 100 -and $results.workflow_state -ne "failed_with_messages")
$results1.Content | Out-File -Append $status_log_path

# The sis import is done, you might do something else here like trigger course copies

Move-Item -Force $outputPath$outputZip $outputPath$t-$outputZip
Remove-Item $sourceDir*.csv

Thank you very much for sharing this.