Johannes
Johannes

Reputation: 440

Outlook Task REST API - Not authorized 401

I am trying to access the Outlook Rest API 2.0 from Powershell to view and create tasks. I created an application and am successfully retrieving the access_token and the required scope (Tasks.ReadWrite). See my response:

token_type     : Bearer
scope          : openid Tasks.ReadWrite
expires_in     : 3600
ext_expires_in : 3600
access_token  :   token_value
id_token  :  id_token_value

Hoever when trying to get a list of tasks i am getting a 401 response (not authorized). See the powershell code below:

$header = @{"Bearer" = "$($response.access_token)"}
$task_list = Invoke-RestMethod -Method Get -Header $Header -Uri" https://outlook.office.com/api/v2.0/me/tasks"

The exception is the following:

Invoke-RestMethod : the remote server returned an error (401) unauthorized
+ $task_list = Invoke-RestMethod -Method Get -Header $Header -Uri "https ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMetho 
   d], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCo 
   mmand

Upvotes: 0

Views: 150

Answers (1)

TToni
TToni

Reputation: 9391

This is not the correct method to transmit a bearer token in the header. I'm no expert on the matter, but RFC 6750 says you should transmit it with an Authorization header with the Bearer method, like this:

$header = @{"Authorization" = "Bearer $($response.access_token)"}

Upvotes: 1

Related Questions