Google 日历 API 服务器到服务器 Rails
Google Calendar API server to server Rails
我想在某些用户操作后在我的日历中插入事件。这就是为什么我想使用 Google API 的服务器到服务器授权。我写了下一个代码:
require 'google/apis/calendar_v3'
require 'googleauth'
require 'google/api_client/client_secrets'
require 'google/api_client'
初始化客户端
client = Google::Apis::CalendarV3::CalendarService.new
加载和解密私钥
key = G.load_from_pkcs12('google_secrets.json', 'notasecret')
生成授权请求正文
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)
获取访问令牌
client.authorization.fetch_access_token!
# load API definition
service = client.discovered_api('calendar', 'v3')
# there is a place for variable "event" declaration
client.execute(:api_method => service.event.insert,
:parameters => { '....' => 'primary' },
:event => event)
但是当我尝试使用该代码时,我收到下一个答案:
cannot load such file -- google/api_client (LoadError)
我知道也许我必须使用以前版本的 gem google-api-client(<0.9) 才能消除该错误。但不知道我必须如何使用 < 0.9 google api 版本的语法制作插入事件。
我找到了解决方案。
使用 api v.0.8.2 并查看下一页:
我想在某些用户操作后在我的日历中插入事件。这就是为什么我想使用 Google API 的服务器到服务器授权。我写了下一个代码:
require 'google/apis/calendar_v3'
require 'googleauth'
require 'google/api_client/client_secrets'
require 'google/api_client'
初始化客户端
client = Google::Apis::CalendarV3::CalendarService.new
加载和解密私钥
key = G.load_from_pkcs12('google_secrets.json', 'notasecret')
生成授权请求正文
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)
获取访问令牌
client.authorization.fetch_access_token!
# load API definition
service = client.discovered_api('calendar', 'v3')
# there is a place for variable "event" declaration
client.execute(:api_method => service.event.insert,
:parameters => { '....' => 'primary' },
:event => event)
但是当我尝试使用该代码时,我收到下一个答案:
cannot load such file -- google/api_client (LoadError)
我知道也许我必须使用以前版本的 gem google-api-client(<0.9) 才能消除该错误。但不知道我必须如何使用 < 0.9 google api 版本的语法制作插入事件。
我找到了解决方案。
使用 api v.0.8.2 并查看下一页: