使用 ruby outlook 创建日历事件

Create calendar event using ruby outlook

我正在尝试在日历中创建一个活动,

我可以按照以下文档获取日历、联系人和电子邮件等所有数据,

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

但是当尝试使用 ruby_outlook 创建事件时出现以下错误

  {"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"}}}}

以下代码用于创建事件

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

您的问题是您获取的令牌正在使用 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 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