通过 Rails 连接到 Google 日历的 API
Connecting to Google Calendar's API through Rails
我正在创建一个非常简单的 Rails 应用程序,具有一个特定目的:将日历事件添加到 Google 日历帐户。
我正在关注 Ruby Guide from the Google Calendar API。
我能够 运行 提供的测试代码(仅 Ruby,没有框架)但是我很难从 Rails 项目访问凭据并且我我不确定正确的("idiomatic"?)方法来组织项目。
部分过程使用 OAuth 2.0,因为此目标需要访问 Google 用户的私人数据(读取和写入),我遵循 Using OAuth 2.0 for Web Services 说明。
现在我有几个关于最佳实践的不同问题 and/or 组织代码的正确方法:
Google 提供了 client_secret.json 具有访问应用程序的凭据。我应该把它放在哪里?我应该将它保存在开发环境中的 .env 文件中,以及(在我的情况下)在生产环境中的 Heroku 的 ENV VARS 中吗?
我尝试将 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)
我创建了一个 services/
文件夹来将 Google 日历相关代码放入其中,但我不确定是否应该将其放入控制器中。我该如何组织?
重要考虑:
我没有使用 Authentication/Authorization 的任何其他方法,例如 Devise 或其他方法,我现在也不打算这样做。我只想获取 Google 的授权令牌并创建一个日历事件。
我能够解决这个问题并将post回答以下每个问题:
One of the possible locations 对于 client_secret.json 文件是 config/client_secret.json
。
在 Heroku 中运送到生产环境时,使用 ENV Vars。
Require 不是在 json 文件中导入凭据的合适方法。
使用Google::APIClient::ClientSecrets.load( File.join( Rails.root, 'config', 'client_secret.json' ) )
(假设文件确实在config/
.
There are several different alternatives 关于如何组织代码。我最终创建了一个服务文件夹和一个 google_calendar.rb class 保存授权逻辑。
代码如下:
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
我正在创建一个非常简单的 Rails 应用程序,具有一个特定目的:将日历事件添加到 Google 日历帐户。
我正在关注 Ruby Guide from the Google Calendar API。
我能够 运行 提供的测试代码(仅 Ruby,没有框架)但是我很难从 Rails 项目访问凭据并且我我不确定正确的("idiomatic"?)方法来组织项目。
部分过程使用 OAuth 2.0,因为此目标需要访问 Google 用户的私人数据(读取和写入),我遵循 Using OAuth 2.0 for Web Services 说明。
现在我有几个关于最佳实践的不同问题 and/or 组织代码的正确方法:
Google 提供了 client_secret.json 具有访问应用程序的凭据。我应该把它放在哪里?我应该将它保存在开发环境中的 .env 文件中,以及(在我的情况下)在生产环境中的 Heroku 的 ENV VARS 中吗?
我尝试将 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)
我创建了一个
services/
文件夹来将 Google 日历相关代码放入其中,但我不确定是否应该将其放入控制器中。我该如何组织?
重要考虑: 我没有使用 Authentication/Authorization 的任何其他方法,例如 Devise 或其他方法,我现在也不打算这样做。我只想获取 Google 的授权令牌并创建一个日历事件。
我能够解决这个问题并将post回答以下每个问题:
One of the possible locations 对于 client_secret.json 文件是
config/client_secret.json
。 在 Heroku 中运送到生产环境时,使用 ENV Vars。Require 不是在 json 文件中导入凭据的合适方法。 使用
Google::APIClient::ClientSecrets.load( File.join( Rails.root, 'config', 'client_secret.json' ) )
(假设文件确实在config/
.There are several different alternatives 关于如何组织代码。我最终创建了一个服务文件夹和一个 google_calendar.rb class 保存授权逻辑。
代码如下:
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