2
votes

I am trying to use the Microsoft.Graph library in order to get access to the Outlook Calendar REST API from the C# code.

In my application I am trying to send a list of events to the Outlook Calendar. After reading the Outlook REST API reference I found that I can't send a list of events in one http request, that's why I connected the Microsoft.Graph library to the project. But when I am trying to get access to the my calendar via graph client I've got an exception with the next message: ServiceException: Code: InvalidAuthenticationToken Message: CompactToken parsing failed with error code: -2147184118

public static string clientId = ConfigurationManager.AppSettings["OutlookCalendarClientId"];
public static string clientSecret = ConfigurationManager.AppSettings["OutlookCalendarClientSecret"];
public static string redirectUri = "https://localhost:56110/appointments/calendar";
public static string scope = ConfigurationManager.AppSettings["OutlookCalendarRWScope"];
public static string accessToken = "EwAgA+l3BAAUWm1xSeJRIJK6txKjBez4GzapzqMAAUOcqDTcOceYJGraa4Q4StlEcWf9iaTwtQECi2/jBcvYwGqwrJ1mTU+ERYp0wJg1ADPAluTd5zkvv6IBxjeUzM6BtONBpFS8ZLaDjNWgNMFKOjAz61F/shGmo4Hp7MER6gYNpxhGwH0RNO0cJsRy7bE4JQWaGro83sZq2NtPBB7qg23OKdeSWVq1ardL9SNHYb4nWWRnm9GNxPGy7nVWK0ZLF7YWNS5rsY34b8MEQetMetVmU0SY7HGDIGrFzmD1YNsBbxO94UGcsoitcjnh8JIWLPflPdLPpbaZCxuoWQhVEZO2TN70SMuJOzixjQQ1tZB62gTlUmP6C36Pug2OMYUDZgAACOCpEMwEMVuu8AFrzisYPh5GC0P+u1ps2//daB+4qoxbqJU8ojACUqd7NGMiZjU1rkCpAuUjLJHMpETZsxETxIed0nA+9FQ4ALuzJmdb/iOZo1es1hil0R8MiYt1kCypfNq6+VWSS8AnKNOPq7hed7E+Zz+V559rLqusPIqMntWGwuK/6Mq4fgMEM4Em/iZ2c1yPcuI6Dqvq5wEdsqWEWS/xSXQQfbRayUZDUMNVoqpfiWTXQ6XBFpXPkZU8Q1+PcerGiGAXPbxe4WIDdmLteS8kATde5aO+RHpe3Dd3Hpqeo+nZKb4jg3ypqW9na9NnRkr+RC+BO5AxIS0CfmIdKIlR9lvRjBmscAncg/3ZH2LXUlwXeQPhp4tJltq7V8bFhAjBWH/d35CV0pUbeqcuvRLq77O1xr375NAH6aS2FHkhuC5zQ38xh7o3lKZRcmdX6Pgcf/m91EC6ktFAVrDN2DnXoFhk+mrhsF4MmiKhNU1SoTWEzBRY3d2Y1hfWAyZYBRJtYqRgNjPCX1NQvOvTuTN9jCbpPxXlrVYpJ76jflyM/CKrjpbTqKLPlF/p3B//8KIacvDK2c2IHYGK/osqlZUXX/I5bsfiNZNRm0UuwOU9xBl8Q5CydFDwmTm/mxfIw+OCCAgJd/fZ0QxI5oyBxfs8l5up+50ZuIYgGAI=";

var client = new GraphServiceClient(
      new DelegateAuthenticationProvider(
          (requestMessage) =>
          {
               requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

               return Task.FromResult(0);
          }));

          var response = client.Me.Calendar.Events.Request().GetAsync().Result;

The access token presented here I got by sending 2 HTTP requests (as mentioned in API reference):

https://login.microsoftonline.com/common/oauth2/v2.0/authorize
?client_id=MY_APPLICATION_ID
&scope=openid+https://outlook.office.com/calendars.readwrite
&redirect_uri=http://localhost:56110/appointments/calendar
&response_type=code

and

https://login.microsoftonline.com/common/oauth2/v2.0/token

The last request was returned an actual access token which I am trying to use with graph client. The most interesting in this case that I could use this token when I call rest services directly (for example, via fiddler) but could not use it in case of Graph client.

And now I am trying to find out why do this happeds, what I am doing wrong and how the access token should be presented to be valid for the Graph Client? Does anybody faced with such problem ?

1
The accessToken that you posted with the code is not valid. The Bearer token should be a JWT token that is in three parts separated by a full stop (.). Could you have cut the header and signature parts by chance? - RasmusW

1 Answers

2
votes

Based on the description, you were using the access_token which for Outlook Calendar REST to call the Microsoft Graph REST.

This will not work since the audience is different for these two REST. To acquiring the access_token for the Microsoft Graph REST for reading the calendar, we can use the Calendars.Read scope. And you can refer the request below:

Get Authorization Code:

GET:https://login.microsoftonline.com/common/oauth2/v2.0/authorize?response_type=code&client_id={client_id}&scope=Calendars.Read%20offline_access&redirect_uri={redirect_uri}

Get Token:

POST:https://login.microsoftonline.com/common/oauth2/v2.0/token
client_id={client_id}&client_secret={client_secret}&grant_type=authorization_code&redirect_uri={redirect_uri}

More detail about the scopes for the Microsoft Graph, you can refer link below:

Microsoft Graph permissions reference