未创建Rails + Google Calendar API事件

时间:2016-08-04 12:05:41

标签: ruby-on-rails ruby google-api google-calendar-api

我创建了rails web app。当用户执行某些操作时,我的应用必须在Google日历中创建新活动。对于我的任务,我选择服务器到服务器授权。

我写下了红宝石代码:

<style>
body
{
    background-color:#4FE1A8;
}
 #wrapper
 {
     margin-top:50px;
 }
 @media screen and (max-width:767px)
 {
     #wrapper
 {
     margin-top:100px;
 }
 }
</style>
<nav class="navbar navbar-default navbar-fixed-top" style="background-color: aliceblue;" role="navigation">
<div class="container-fluid">
    <div class="navbar-header">
        <img class="img-responsive" src="img/logo.png" style="width: 330px; height: 49px;" />
    </div>
    <ul class="nav navbar-nav navbar-right" id="navbarprop">
        <li id="home" class="active">
            <a class="color" href="#"><i class="glyphicon glyphicon-home"></i>&nbsp Home</a>
        </li>              
    </ul>
</div>
</nav>
<div class="container" id="wrapper">
<h1>HELLO</h1>
</div>

但是当我尝试使用该代码并且服务器将结果作为JSON返回时,如果我继续使用&#34;创建&#34;事件,谷歌向我显示该事件不存在的消息。

  client = Google::APIClient.new({:application_name => "Rails calendar",
                         :application_version => "1.0"})

  keypath = Rails.root.join('config','google_secrets.p12').to_s
  key = Google::APIClient::PKCS12.load_key(keypath, "notasecret")

# generate request body for authorization
  client.authorization = Signet::OAuth2::Client.new(
      :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
      :audience             => 'https://accounts.google.com/o/oauth2/token',
      :scope                => 'https://www.googleapis.com/auth/calendar',
      :issuer               => '***',
      :signing_key          => key).tap{ |auth| auth.fetch_access_token! }


  api_method = client.discovered_api('calendar','v3').events.insert
  @meeting_data = {
      guests: [{email: '***@gmail.com'}, {email: '***@gmail.com'}],
      start_time: "2016-08-10T19:00:00+03:00",
      end_time: "2016-08-10T20:00:00+03:00",
      topic: "Test meeting",
      messages_thread_id: 2
  }

  event = {
      summary: @meeting_data[:topic],
      start: {
          dateTime: @meeting_data[:start_time],
          time_zone: 'Asia/Jerusalem'
      },
      end: {
          dateTime: @meeting_data[:end_time],
          time_zone: 'Asia/Jerusalem'
      },
      attendees: @meeting_data[:guests],
      visibility: 'private',
      reminders: {
          useDefault: true,
      }
  }

  result = client.execute(:api_method => api_method,
                          :parameters => {calendarId: 'primary'},
                          :body => JSON.dump(event),
                          :headers => {'Content-Type' => 'application/json'})

  puts result.data.as_json

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。需要为日历的“client_email”提供写入规则。

您使用组织日历。这就是为什么只有组织管理员才能做到这一点。