ruby-on-rails - 如何使用 Ruby 客户端为服务器到服务器应用程序设置 Google Calendar API

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

我花了一些时间阅读了几个地方,并查看了 Stack Overflow 以弄清楚如何在服务器到服务器应用程序中将 Google API Ruby 客户端与 Google Calendar 一起使用,而不让服务器客户端完全访问所有用户。我只是希望它能够获取/创建/更新/删除单个日历的事件。可能还有其他方法可以做到这一点,但我在下面记录了我是如何做到的,因为它可能会帮助其他人。

最佳答案

谷歌日历的授权设置

有点帮助的链接:https://developers.google.com/api-client-library/ruby/

  1. 转到:https://console.developers.google.com/iam-admin/projects
  2. 点击“+创建项目”
  3. 输入“项目名称”和“项目 ID”,然后点击“创建”
  4. 在“库”下选择“日历 API”
  5. 点击“>启用”
  6. 返回:https://console.developers.google.com/iam-admin/projects
  7. 点击“服务帐户”
  8. 点击“选择项目”
  9. 选择您的项目并点击“打开”
  10. 点击“+创建服务帐户”
  11. 输入“服务帐户名称”并选择“角色”(我选择“编辑”)
  12. 选中“提供新的私钥”
  13. 点击“创建”

JSON 文件将下载到您的计算机。 将此文件移动到您的应用程序可以访问的某个位置,并将其重命名为“google_api.json”(或者您想要的任何名称,只要它与下面的正确路径匹配即可)。确保只有应用程序可以访问此文件(它包含一个私钥)。

  1. 从 JSON 文件中复制“client_email”并转到 您希望此应用程序访问的 Google 日历。
  2. 点击“日历”
  3. 选择正确的日历
  4. 点击“日历地址”下的“更改共享设置”:
  5. 添加您复制的电子邮件并选择适当的权限
  6. 点击“保存”
  7. 复制“日历地址”右侧的“日历 ID”:
  8. 您可以对下面的日历 ID 进行硬编码,或者将其放入 YAML 文件或作为环境变量。

这是授权和访问 Googe Calendar API 的示例文件:

# http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/CalendarV3
require 'googleauth'
require 'google/apis/calendar_v3'

class MyApp::GoogleCalendar

  def initialize
    authorize
  end

  def service
    @service
  end
  
  def events(reload=false)
    # NOTE: This is just for demonstration purposes and not complete.
    # If you have more than 2500 results, you'll need to get more than    
    # one set of results.
    @events = nil if reload
    @events ||= service.list_events(calendar_id, max_results: 2500).items
  end

private

  def calendar_id
    @calendar_id ||= # The calendar ID you copied in step 20 above (or some reference to it).  
  end

  def authorize
    calendar = Google::Apis::CalendarV3::CalendarService.new
    calendar.client_options.application_name = 'App Name' # This is optional
    calendar.client_options.application_version = 'App Version' # This is optional

    # An alternative to the following line is to set the ENV variable directly 
    # in the environment or use a gem that turns a YAML file into ENV variables
    ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "/path/to/your/google_api.json"
    scopes = [Google::Apis::CalendarV3::AUTH_CALENDAR]
    calendar.authorization = Google::Auth.get_application_default(scopes)

    @service = calendar
  end

end

现在您可以调用 cal = MyApp::GoogleCalendar.new 并使用 cal.events 获取事件。或者您可以直接使用 cal.service.some_method(some_args) 进行调用,而不是在此文件中创建方法。

关于ruby-on-rails - 如何使用 Ruby 客户端为服务器到服务器应用程序设置 Google Calendar API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40722687/

相关文章:

ruby-on-rails - 如何在 Ruby on Rails 中获取特殊字符之间的内容?

ruby-on-rails - 这每次都会影响数据库吗?

ruby-on-rails - Rails 3 - 如果变量包含 URL 的一部分?

ios - FaSTLane:似乎没有为此项目设置 CURRENT_PROJECT_VERSION 键

ruby-on-rails - 如何在 Rails 4.1 应用程序中使用基于事件的文件系统监听器和 Spring

jquery - 在 FullCalendar + Google 日历中隐藏没有描述的事件

java - 为什么我在使用 java 的 Google CAlendar 库中遇到此异常?

javascript - 如何从应用程序脚本中使用 googleapis

ruby-on-rails - Rails 对 :dependent => :destroy and cascade delete/nullify/restrict 有什么作用

ruby-on-rails - 使用集合向 Formtastic 单选按钮添加背景图像或图像标签