【问题标题】:Connecting to Google Calendar's API through Rails通过 Rails 连接到 Google Calendar API
【发布时间】:2017-01-03 17:46:29
【问题描述】:

我正在创建一个非常简单的 Rails 应用程序,其目的有一个:将日历事件添加到 Google 日历帐户。

我正在关注Ruby Guide from the Google Calendar API

我能够运行提供的测试代码(仅限 Ruby,无框架),但我很难从 Rails 项目中访问凭据,而且我不确定正确的(“惯用”?)方法去做并组织项目。

该过程的一部分是使用 OAuth 2.0,因为此目标需要访问 Google 用户的私人数据(读取和写入),我正在遵循Using OAuth 2.0 for Web Services 的说明。

现在我有几个关于最佳实践和/或组织代码的正确方法的不同问题:

  1. Google 提供了一个 client_secret.json,它具有访问应用程序的凭据。我应该把它放在哪里?我应该将它保存在开发环境中的 .env 文件中,以及(在我的情况下)生产环境中 Heroku 的 ENV VARS 中吗?

  2. 我尝试将 client_secret.json 文件保存在项目的根文件夹中(与 Gemfile 的路径相同),将其添加到 .gitignore 但我无法require "#{Rails.root}/client_secret.json"

    /Users/jsoifer/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require': No such file to load -- /Users/jsoifer/Developer/Tiny-Things/tiny-booking/client_secret.json (LoadError)
    
  3. 我创建了一个 services/ 文件夹来将 Google 日历相关代码放入其中,但我不确定是否应该将其放入控制器中。我应该如何组织这个?

重要考虑: 我没有使用任何其他身份验证/授权方法,例如 Devise 或其他方法,我现在不打算这样做。我只想获取 Google 的授权令牌并创建一个日历事件。

Github Project Link

【问题讨论】:

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


    【解决方案1】:

    我能够弄清楚这一点,并将发布以下每个问题的答案:

      client_secret.json 文件的
    1. One of the possible locationsconfig/client_secret.json。 在 Heroku 中运送到生产环境时,请使用 ENV Vars。

    2. Require 不是在 json 文件中导入凭据的适当方式。 使用Google::APIClient::ClientSecrets.load( File.join( Rails.root, 'config', 'client_secret.json' ) )(假设文件确实在config/

    3. There are several different alternatives 关于如何组织代码。我最终创建了一个服务文件夹和一个包含授权逻辑的 google_calendar.rb 类。

    代码如下:

    app/services/google_calendar.rb

    require 'google/api_client/client_secrets'
    require 'google/apis/calendar_v3'
    
    class GoogleCalendar 
      # Attributes Accessors (attr_writer + attr_reader)
      attr_accessor :auth_client, :auth_uri, :code
    
      def initialize
    
        # ENV: Development
        # Google's API Credentials are in ~/config/client_secret.json
        client_secrets = Google::APIClient::ClientSecrets.load( File.join( Rails.root, 'config', 'client_secret.json' ) )
        @auth_client = client_secrets.to_authorization
    
        # Specify privileges and callback URL
        @auth_client.update!(
          :scope => 'https://www.googleapis.com/auth/calendar',
          :redirect_uri => 'http://localhost:3000/oauth2callback'
        )
    
        # Build up the Redirecting URL
        @auth_uri = @auth_client.authorization_uri.to_s
    
      end
    
    end
    

    app/controllers/application_controller.rb

    class ApplicationController < ActionController::Base
      protect_from_forgery with: :exception
    
      # Starting action in config/routes.rb
      def welcome
    
        # Redirect to Google Authorization Page
        redirect_to GoogleCalendar.new.auth_uri
    
      end
    
      def token
        # Get a auth_client object from Google API
        @google_api = GoogleCalendar.new
    
        @google_api.auth_client.code = params[:code] if params[:code]
        response = @google_api.auth_client.fetch_access_token!
    
        session[:access_token] = response['access_token']
    
        # Whichever Controller/Action needed to handle what comes next
        redirect_to new_event_path()
      end
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 2012-04-19
      • 2016-10-23
      • 2014-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多