AnsweredAssumed Answered

I am getting redirected when trying to set notification preferences via the API using PowerShell

Question asked by bniessen@trinity.nsw.edu.au on May 5, 2015
Latest reply on May 10, 2015 by bniessen@trinity.nsw.edu.au

The following works - I get the notification preferences for the specified user:

 

$url = "https://subdomain.test.instructure.com/api/v1/users/1/communication_channels/email/user@email.com/notification_preferencesinstructure.com/api/v1/users/1/communication_channels/email/user@email.com/notification_preferences"$access_token = ""$headers = @{"Authorization"=$("Bearer " + $access_token)}$request = Invoke-RestMethod -Uri $url -Headers $headers -Method GET

 

I create an object to express my desired notifcation preferences in PowerShell and convert it to JSON:

 

$json_never_prefs = @{new_account_user = "never"; assignment_created = "never";}$json_never_prefs = $json_never_prefs | ConvertTo-Json

 

 

Then I attempt to put the new preferences at the web endpoint - as per documentation: Canvas LMS REST API Documentation

 

$request = Invoke-RestMethod -Uri $url -Headers $headers -Method PUT -Body $json_never_prefs -ContentType "application/json"

>> This tells me I am going to be redirected - WHY?

 

If I change it to a Invoke-WebReuqest, I can get the redirection URL: "https://subdomain.test.instructure.com/login?needs_cookies=1"
>> But I am using the access token?

 

If I grab a session variable as per below, I still can't update the preferences:

$request = Invoke-WebRequest -Uri $url -Headers $headers -Method GET -SessionVariable session$request = Invoke-WebRequest -Uri $url -Method GET -WebSession $session

>> $request is a 200 response here with the expected notification preferences response (using no $headers and instead using the session variable)

 

When I attempt to do a PUT using the session variable though, I get the following:

$request = Invoke-WebRequest -Uri $url -Method PUT -Body $json_never_prefs -ContentType "application/json" -WebSession $session

 

>>Invoke-WebRequest : Skip To Content My Dashboard Log In

Help

Close

Page Error

There was a problem with your last request. You may have tried to perform an action after a long period of inactivity. If that's the case,try going back, reloading the page you were working on, and resubmitting. If not, please let us know what you were doing when the errorhappened.

Click here to tell us what happened

    Email (optional):

Send Comments

By Instructure User Research Help Privacy policy Terms of service Facebook Twitter

In order to create video or audio recordings your computer needs to be webcam-enabled. If you don't have a webcam on your computer, youcan still record audio-only messages by first installing the Google Video Chat plugin.

Install the Video Plugin

Don't have a webcam?

At line:1 char:12+ $request = Invoke-WebRequest -Uri $url -Method PUT -Body $json_never_ ...

+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException

>>

 

 

Any help would be appreciated as I have been tasked with changing all observers notification preferences via the API and can not figure out how to do this currently.

 

Thanks in advance,

 

Brad

 

Message was edited by: Brad Niessen code tags for formatting

Outcomes