0

I am trying create a event in calendar,

Iam able get all the data like calendar,contacts and emails by following below documentaion,

https://learn.microsoft.com/en-us/outlook/rest/ruby-tutorial,

But when try to create a event using ruby_outlook getting below error

  {"ruby_outlook_error"=>401, 
   "ruby_outlook_response"=>{"error"=>{"code"=>"InvalidAudience", "message"=>"The audience claim value is invalid 'aud'.", 
   "innerError"=>{"requestId"=>"75984820-5241-11ea-b6fc-fc4dd44c1550", "date"=>"2020-02-18T11:26:08"}}}}

Below code is for creating event

def def index
   token = get_access_token   //getting access token
   if token
      outlook_client = RubyOutlook::Client.new
      event_payload = 
      {
         "Subject": "Discuss the Calendar REST API",
        "Body": {
        "ContentType": "HTML",
        "Content": "I think it will meet our requirements!"
        },
        "Start": {
        "DateTime": "2020-03-03T18:00:00",
        "TimeZone": "Pacific Standard Time"
        },
        "End": {
        "DateTime": "2020-03-03T19:00:00",
        "TimeZone": "Pacific Standard Time"
        },
        "Attendees": [
        {
        "EmailAddress": {
        "Address": "john@example.com",
        "Name": "John Doe"
        },
        "Type": "Required"
        }
        ]
      }
      outlook_client.create_event(token, event_payload, nil, 'user@domain.com')
   end
end
barnacle.m
  • 2,070
  • 3
  • 38
  • 82
Manjunarth
  • 313
  • 2
  • 7
  • 22
  • I think your issue is that the token that you fetched was using the [microsoft graph API](https://learn.microsoft.com/en-us/graph/overview) but now you are trying to create an even through the Outlook API. You cannot use a token issued for Graph ("aud": "https://graph.microsoft.com") against the Outlook endpoint. You need a token with "aud": "https://outlook.office.com". – Gautam Feb 18 '20 at 11:36
  • Hi @Gautam, thanks for your reply on this, How can I get token with "aud":"outlook.office.com" – Manjunarth Feb 18 '20 at 11:43
  • Why do you want to do that? Better is use the graph API itself to create an event since you already have the token fetched from it. Let me know if you can use the [graph gem](https://github.com/microsoftgraph/msgraph-sdk-ruby)? I have working code for it and I can post it as the asnwer. – Gautam Feb 18 '20 at 11:46
  • Yes that's goot idea, but I did not find code to create events in [https://learn.microsoft.com/en-us/outlook/rest/ruby-tutorial] documentaion, this is about fetchin data. – Manjunarth Feb 18 '20 at 11:50
  • Yes @Gautam, I am using **microsoft_graph** gem – Manjunarth Feb 18 '20 at 12:01
  • I have posted an answer for it. – Gautam Feb 18 '20 at 12:09

1 Answers1

2

Your issue is that the token that you fetched was using the Microsoft graph API but now you are trying to create an even through the Outlook API. You cannot use a token issued for Graph ("aud": "graph.microsoft.com") against the Outlook endpoint. You need a token with "aud": "outlook.office.com".Better is use the graph API itself using the graph gem to create an event since you already have the token fetched from it.

To do that first create the MicrosoftGraph object

def create_service_auth
  access_token = get_access_token
  callback =  Proc.new do |r|
    r.headers['Authorization'] = "Bearer #{access_token}"
    r.headers['Content-Type']  = 'application/json'
    r.headers['X-AnchorMailbox'] = "#{ email_of_calendar_for_which_to_create_the_event }"
  end
  @graph = ::MicrosoftGraph.new(base_url: 'https://graph.microsoft.com/v1.0',
                            cached_metadata_file: File.join(MicrosoftGraph::CACHED_METADATA_DIRECTORY, 'metadata_v1.0.xml'),
                              &callback)
end

Then create the event -

def create_event
  event = {
    subject: summary,
    body: {
      content_type: "HTML",
      content: description
    },
    start: {
      date_time: start_time,
      time_zone: timezone
    },
    end: {
      date_time: end_time,
      time_zone: timezone
    },
    response_requested: true,
    organizer: {emailAddress: { name: "#{organizer.full_name}", address: email_of_calendar_for_which_to_create_the_event }},
    attendees: [
      {
        email_address: {
          address: attendee_email,
          name: "#{attendee.full_name}"
        },
        type: "required"
      }
    ]
  }

  result = @graph.me.events.create(event)
end
Gautam
  • 1,754
  • 1
  • 14
  • 22