创建Google事件后如何在收到的电子邮件通知中删除GoogleMeet?

时间:2020-05-21 10:23:11

标签: ruby google-calendar-api

enter image description here

class SendInvitation
  def self.new(match_info,current_user)

    begin

      authorization = GoogleRefresh.auth_client(current_user)
      service = Google::Apis::CalendarV3::CalendarService.new
      service.authorization = authorization

      match_date = match_info.match_date.strftime("%F")
      min_time = match_info.min_time.strftime("%T%:z")
      max_time = match_info.max_time.strftime("%T%:z")
      start_time = "#{match_date}T#{min_time}"
      end_time = "#{match_date}T#{max_time}"

      minutes = ((start_time.to_time-Time.now) / 60).to_i

      attendeesList = Match.find(id=match_info.id).users.map{|user| Google::Apis::CalendarV3::EventAttendee.new(email: "#{user.email}")}
      attendeesList<<Google::Apis::CalendarV3::EventAttendee.new(email: "#{User.find(id=match_info.owner_id).email}")



      event = Google::Apis::CalendarV3::Event.new(
          summary: "CalendarMatcher -#{match_info.title}",
          location: match_info.location,
          description: "Descritption: #{match_info.description}",


           start: Google::Apis::CalendarV3::EventDateTime.new(
            date_time: start_time,
            time_zone: 'Europe/Brussels'
            ),
          end: Google::Apis::CalendarV3::EventDateTime.new(
            date_time: end_time,
            time_zone: 'Europe/Brussels'
            ),

        attendees:  attendeesList,

        reminders: Google::Apis::CalendarV3::Event::Reminders.new(
          use_default: false,
          overrides: [
            Google::Apis::CalendarV3::EventReminder.new(
              reminder_method: 'email',
              minutes: minutes
              ),
            Google::Apis::CalendarV3::EventReminder.new(
              reminder_method: 'popup',
              minutes: 30
              )
          ]
          )
        )


        result = service.insert_event('primary', event)



        puts "Event created: #{updated_result.html_link}"

      rescue ::Google::Apis::AuthorizationError => e
      authorization.grant_type = 'refresh_token'
      authorization.access_token = current_user.token
      authorization.refresh_token = current_user.refresh_token
      response = authorization.refresh!
      current_user.token = response['access_token']
      current_user.save

      retry
    end

  end
end

我正在努力使用google api文档...我是一名非常初级的开发人员,希望有人能够为我提供帮助。

这是将电子邮件发送给与会者并将事件添加到与会者日历中的所有方法。运行正常:它发送电子邮件并填写日历。

但是电子邮件中包含我要删除的GoogleMeet链接。 因此,我阅读了互联网上的所有文档。据我了解,GoogleMeet会响应一些“会议数据”设置...

但是当我开始添加Conference_data_version时:1。 result = service.insert_event('primary', event, conference_data_version: 1); 我收到一条错误消息,指出“未知关键字:conference_data_version”

我被卡住了(很明显;))。

当您刚开始编写代码时,Google文档非常困难……我希望这将有助于更好地了解我在寻找什么。

1 个答案:

答案 0 :(得分:0)

答案:

您需要将conference_data_version设置为1

代码更改:

result = service.insert_event('primary', event, conference_data_version: 0)

应更改为:

result = service.insert_event('primary', event, conference_data_version: 1)

此外,您无需在事件资源中指定一个空的conference_data: {}对象。


这是我的完整代码,因此您可以看到它是如何完成的。

authorize函数来自Google提供的Ruby Quickstart,并且Calendar插入根据客户端文档进行了修改:

require "google/apis/calendar_v3"
require "googleauth"
require "googleauth/stores/file_token_store"
require "date"
require "fileutils"

OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
APPLICATION_NAME = "Google Calendar API Ruby Quickstart".freeze
CREDENTIALS_PATH = "credentials.json".freeze
# The file token.yaml stores the user 's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.

TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR

##
# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization.If authorization is required, 
# the user 's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials

def authorize
  client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
  token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
  authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
  user_id = "default"
  credentials = authorizer.get_credentials user_id
  if credentials.nil ?
    url = authorizer.get_authorization_url base_url : OOB_URI
    puts "Open the following URL in the browser and enter the "\
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

# Initialize the API
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

event = Google::Apis::CalendarV3::Event.new(
    summary: "CalendarMatcher -#{match_info.title}",
    location: match_info.location,
    description: "Descritption: #{match_info.description}",
    start: Google::Apis::CalendarV3::EventDateTime.new(
        date_time: start_time,
        time_zone: 'Europe/Brussels'
    ),
    end: Google::Apis::CalendarV3::EventDateTime.new(
        date_time: end_time,
        time_zone: 'Europe/Brussels'
    ),
    attendees: [
        Google::Apis::CalendarV3::EventAttendee.new(
            email: 'email@domain.com'
        ),
        Google::Apis::CalendarV3::EventAttendee.new(
            email: 'email2@domain.com'
        )
    ],
    reminders: Google::Apis::CalendarV3::Event::Reminders.new(
        use_default: false,
        overrides: [
            Google::Apis::CalendarV3::EventReminder.new(
                reminder_method: 'email',
                minutes: minutes
            ),
            Google::Apis::CalendarV3::EventReminder.new(
                reminder_method: 'popup',
                minutes: 30
            )
        ]
    )
)
result = service.insert_event('primary', event, conference_data_version: 1)

请确保删除您的token.yaml文件,然后重新运行程序。

我希望这对您有帮助!