Manjunarth
Manjunarth

Reputation: 323

Create calendar event using ruby outlook

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": "[email protected]",
        "Name": "John Doe"
        },
        "Type": "Required"
        }
        ]
      }
      outlook_client.create_event(token, event_payload, nil, '[email protected]')
   end
end

Upvotes: 0

Views: 959

Answers (1)

Gautam
Gautam

Reputation: 1812

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

Upvotes: 2

Related Questions