Share via


Windows Powershell Error - Invoke-RestMethod : Sendera:UnauthorizedUnauthorized

Question

Wednesday, April 5, 2017 1:46 PM

installed O365ServiceCommunication module and tried executing script but I am getting following error. Please help.

Invoke-RestMethod : Sendera:UnauthorizedUnauthorized
At C:\windows\system32\windowspowershell\v1.0\Modules\O365ServiceCommunications\1.2\functions\New-SCSession.ps1:59 char:22
+         Cookie     = Invoke-RestMethod @Splat | Select-Object -Expand ...
+                      ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Invoke-RestMethod : Sendera:BadRequestBad Request
At C:\windows\system32\windowspowershell\v1.0\Modules\O365ServiceCommunications\1.2\functions\Get-SCEvent.ps1:79 char:3
+         Invoke-RestMethod @Splat | Select-Object -ExpandProperty Even ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand 

All replies (11)

Wednesday, April 5, 2017 6:07 PM

You are not authenticating correctly.

\(ツ)_/


Wednesday, April 5, 2017 6:16 PM

I am global admin in Office 365, I am entering the right credentials but still getting this error. What could be the reason?

Pyarelal

 


Wednesday, April 5, 2017 6:46 PM

Without correct information there is no way to guess.  You must use correct authorization methods with OAuth.  I recommend reviewing the examples for how to use the module and how to persist authentication.

\(ツ)_/


Wednesday, April 5, 2017 7:56 PM

Payarelal -

I'm familiar with the module your using.

What happens when you run the following commands:

$cred = get-credentials

$session = New-SCSession -Credential $creds


Wednesday, April 5, 2017 8:41 PM

Adam

When I am using my federated Global Admin account I am getting the above mentioned error. But when I am using Cloud account having User Management Admin rights it is working.

My AD account is on-premise and is synced with Office 365 through DirSync, I am Global Administrator but nor working.

Getting Error: Invoke-RestMethod : Sendera:UnauthorizedUnauthorized

 


Wednesday, April 5, 2017 9:23 PM

Can you try the raw restful method below and let me know if you are still receiving an authentication error:

$creds = Get-Credentials

$O365Creds = (@{userName=$creds.username;password=$Creds.GetNetworkCredential().password;} | convertto-json).tostring()
$Authtoken = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register" -body $O365Creds -ErrorAction Stop).RegistrationCookie

$Authsession = (@{lastCookie=$Authtoken;locale="en-US";preferredEventTypes=@(0,1)} | convertto-json).tostring()

$events = (invoke-restmethod -contenttype "application/json" -method Post -uri "https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents" -body $Authsession)

$events = (($events |? {$_.eventinfotype -eq 1}).events)

$events


Thursday, April 6, 2017 12:51 PM

Yes Adam I am using your script, but as of now I am trying to run Get-SCEvent command to test my credentials but I am still getting below error am I doing something wrong. Also please let me know where to enter the recipient email address in your script.   

$creds=Get-Credential

$O365Creds=(@{userName=$creds.username;password=$Creds.GetNetworkCredential().password;} |convertto-json).tostring()

$Authtoken=(invoke-restmethod-contenttype"application/json"-methodPost-uri"https://api.admin.microsoftonline.com/shdtenantcommunications.svc/Register"-body$O365Creds-ErrorActionStop).RegistrationCookie

$Authsession=(@{lastCookie=$Authtoken;locale="en-US";preferredEventTypes=@(0,1)} |convertto-json).tostring()

$events=(invoke-restmethod-contenttype"application/json"-methodPost-uri"https://api.admin.microsoftonline.com/shdtenantcommunications.svc/GetEvents"-body$Authsession)

$events=(($events|?{$_.eventinfotype -eq1}).events)

$events

#$creds = Get-Credential

$session

=New-SCSession-Credential$O365Creds

Get-SCEvent

-SCSession$session|sortstartTime-Descending# -EventTypes Incident

PS C:\WINDOWS\system32> $session = New-SCSession -Credential $O365Creds
Get-SCEvent -SCSession $session | sort startTime -Descending
Invoke-RestMethod : Sendera:UnauthorizedUnauthorized
At C:\windows\system32\windowspowershell\v1.0\Modules\O365ServiceCommunications\1.2\functions\New-SCSession.ps1:59 char:22
+         Cookie     = Invoke-RestMethod @Splat | Select-Object -Expand ...
+                      ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Invoke-RestMethod : Sendera:BadRequestBad Request
At C:\windows\system32\windowspowershell\v1.0\Modules\O365ServiceCommunications\1.2\functions\Get-SCEvent.ps1:79 char:3
+         Invoke-RestMethod @Splat | Select-Object -ExpandProperty Even ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

PS C:\WINDOWS\system32>


Monday, July 31, 2017 2:46 PM

I am having the exact same problem using an Global Admin account that is also in AD and synced with DirSync.   Did you ever find a solution or work around for this?


Monday, July 31, 2017 2:57 PM

I do not know whether it is a solution or workaround, but I used an account created in Office365, which is not synced with AD and having Global Admin Role access. It worked with office 365 user account.   

Pyarelal Devadas Messaging & Collaboration Support


Monday, July 31, 2017 3:08 PM

Thanks for the quick reply.  I was fearing that was going to be the answer as I saw in the above thread you had mentioned having some success with that.  What kind of permissions did you find were required for the O365 account?


Friday, November 30, 2018 6:41 AM

Disable MFA and try again. it should work