Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在我的GSuite日历上创建活动,并使用服务帐户邀请与会者_Ruby On Rails_Ruby_Google Cloud Platform_Google Api Client_Google Developers Console - Fatal编程技术网

Ruby on rails 在我的GSuite日历上创建活动,并使用服务帐户邀请与会者

Ruby on rails 在我的GSuite日历上创建活动,并使用服务帐户邀请与会者,ruby-on-rails,ruby,google-cloud-platform,google-api-client,google-developers-console,Ruby On Rails,Ruby,Google Cloud Platform,Google Api Client,Google Developers Console,我不知道我做错了什么。试图通过google服务帐户邀请某人参加活动 第一步-我去开发人员控制台添加了一个服务帐户,得到了密钥(存储在下面代码中的better.json中),给了它日历API权限,它还得到了oath2权限: 第二步-我给了我的服务帐户“GSuite域范围授权” 第三步-在GSuite端,我登录到我的管理面板,找到了这个小家伙并进行了适当的更新。我想?范围?: 最后但并非最不重要的一点是,下面是我正在运行的代码 require 'googleauth' scope = 'ht

我不知道我做错了什么。试图通过google服务帐户邀请某人参加活动

第一步-我去开发人员控制台添加了一个服务帐户,得到了密钥(存储在下面代码中的better.json中),给了它日历API权限,它还得到了oath2权限:

第二步-我给了我的服务帐户“GSuite域范围授权”

第三步-在GSuite端,我登录到我的管理面板,找到了这个小家伙并进行了适当的更新。我想?范围?:

最后但并非最不重要的一点是,下面是我正在运行的代码

require 'googleauth'

scope = 'https://www.googleapis.com/auth/calendar'

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('better.json'),
  scope: scope)

authorizer.fetch_access_token!

service = Google::Apis::CalendarV3::CalendarService.new
service.authorization = authorizer

calendar_id = 'primary'



event = Google::Apis::CalendarV3::Event.new(
  summary: 'douchin',
  location: '800 Howard St., San Francisco, CA 94103',
  description: 'A chance to hear more about Google\'s developer products.',
  start: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2020-04-06T09:00:00-07:00',
    time_zone: 'America/New_York'
  ),
  end: Google::Apis::CalendarV3::EventDateTime.new(
    date_time: '2020-04-06T10:00:00-07:00',
    time_zone: 'America/New_York'
  ),
  recurrence: [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  guestsCanModify: 'TRUE',
  attendees: [
    Google::Apis::CalendarV3::EventAttendee.new(
      email: 'brianthebiologist@gmail.com'
    )
  ],
  reminders: Google::Apis::CalendarV3::Event::Reminders.new(
    use_default: false,
    overrides: [
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'email',
        minutes: 24 * 60
      ),
      Google::Apis::CalendarV3::EventReminder.new(
        reminder_method: 'popup',
        minutes: 10
      )
    ]
  )
)

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

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


response = service.list_events(calendar_id,
                               max_results: 10,
                               single_events: true,
                               order_by: 'startTime',
                               time_min: Time.now.iso8601)
puts 'Upcoming events:'
puts 'No upcoming events found' if response.items.empty?
response.items.each do |event|
  start = event.start.date || event.start.date_time
  puts "- #{event.summary} (#{event.html_link})"
end
我有两个问题

1) 如果我试图创建一个有与会者的活动(这是我真正想要的),我会得到一个错误:forbiddenForServiceAccounts:Service accounts如果没有域范围的授权,就不能邀请与会者。(Google::API::ClientError)


2) 如果我尝试创建一个没有任何与会者的活动,它会说它可以工作,并将信息存储在某个神奇的日历中……但当我登录到谷歌上的管理员帐户时,它不会显示在我的GSuite日历中。所以我的auth工作了..我想..它回退了一个事件链接,但是当我尝试转到它时,我得到了另一个错误:“在谷歌日历ui上找不到请求的事件”。

好的-解决了这个问题。我只需要使用Signet来生成令牌,而不是我正在使用的任何令牌,下面的示例中,privatekey=我的服务帐户私钥cred:

signing_key = OpenSSL::PKey::RSA.new(privatekey.gsub("\\n", "\n"), 'notasecret')
authorizer = Signet::OAuth2::Client.new(
  {
    signing_key: signing_key,
    token_credential_uri: "https://accounts.google.com/o/oauth2/token",
    audience: "https://accounts.google.com/o/oauth2/token",
    issuer: ENV['service_account_email'],
    person: ENV['email_on_my_domain_i_want_to_impersonate'],
    scope: scope
  })

token = authorizer.fetch_access_token!
希望有一天这能帮助别人!如果有,请投票