cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ross_gailer
Community Participant

Creating user via API

I have had a powershell script working for creating users in Canvas, but now for some reason it is broken and I can not see why. I have added lots of debugging to check that I am actually getting the json data built correctly and it does all appear to be there. I am getting 400 errors.

Any help with what has gone wrong with this would be appreciated

#Now we create the Canvas user......
#Set the headers
$headers = @{
Authorization= "Bearer <token>"
}
$url = "https://<our instance>.instructure.com/api/v1/accounts/1/users"
#Create the body with the variable data in it
$jsontxt = @"
{
"user": {
"name": "$name",
"short_name": "$givenName"
},
"pseudonym": {
"unique_id": "$mail",
"sis_user_id": "$sam",
"authentication_provider_id": "2"
},
"communication_channel": {
"type": "email",
"address": "$mail"
}
}
"@
$method = "POST"
#Lets see if we can build a call
#check the url
write-host $url
write-host $headers
write-host $jsontxt
try {
Invoke-RestMethod -Uri $url -Method POST -Header $headers -ContentType "application/json" -Body $jsontxt
} catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
Write-Host $_.Exception
}

Labels (1)
Tags (2)
0 Kudos
3 Replies
pamedmoston
Community Participant

Hello, I'm having the same problem, did you ever get this resolved?

Thank you!

ross_gailer
Community Participant

Yes I did. A weird thing really, for one of the variables that I was calling in the script had not been declared correctly. This is part of a larger script and as part of the loop that this goes through, I had been clearing variables to avoid one being carried over to the next record if it was blank in the file I was reading from. When I cleared the variable, it was not reset as part of the setup for the loop. Turns out it was one of the required variables for the API and as it was missing, I was getting the 400 error code. Updated the script to declare and fill the variable correctly and it worked after that. From memory, it was the $mail variable in this script.

By the time I found it I had forgotten I had even logged this I must admin, apologies for not getting back on here and updating it.

pamedmoston
Community Participant

Thanks for the update. The only way I was able to get mine to work was by sending the parameters as url variables instead of in the request body (support person told me to do that). Weird, but it worked.