ruby-on-rails - Google 日历服务帐户 API 导致授权无效

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

我从

中获取了示例代码
http://code.google.com/p/google-api-ruby-client/source/browse/service_account/analytics.rb?repo=samples

然而,我一直在努力让它发挥作用,但无论我多么努力,它都在不断地给予!这是我不断收到的错误

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/signet-0.4.5/lib/signet/oauth_2/client.rb:875:in `fetch_access_token': Authorization failed.  Server message: (Signet::Authorization
Error)
{
  "error" : "invalid_grant"
}
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/signet-0.4.5/lib/signet/oauth_2/client.rb:888:in `fetch_access_token!'

我该如何解决这个问题?

这是我的完整代码

    require 'google/api_client'
require 'date'
require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

# Update these to match your own apps credentials
service_account_email = 'xxxxxx' # Email of service account
key_file = 'privatekey.p12' # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
profileID = 'xxxxx' # Analytics profile ID.


client = Google::APIClient.new()

# Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
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/analytics.readonly',
  :issuer => service_account_email,
  :signing_key => key)

# Request a token for our service account
client.authorization.fetch_access_token!

analytics = client.discovered_api('analytics','v3')

startDate = DateTime.now.prev_month.strftime("%Y-%m-%d")
endDate = DateTime.now.strftime("%Y-%m-%d")

visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => { 
  'ids' => "ga:" + profileID, 
  'start-date' => startDate,
  'end-date' => endDate,
  'dimensions' => "ga:day,ga:month",
  'metrics' => "ga:visits",
  'sort' => "ga:month,ga:day" 
})

print visitCount.data.column_headers.map { |c|
  c.name  
}.join("\t")

visitCount.data.rows.each do |r|
  print r.join("\t"), "\n"
end

我花了一整天的时间来完成这个工作。请帮忙。谢谢你的时间。

最佳答案

解决了!

我的电脑时钟由于某种原因晚了 4 个小时!修正了系统时间,不会导致错误。

1 - 使用 Google API 常见问题解答来解决 SSL 错误(如果有)。

这花了我一整天的时间,所以我要留下我的解决方案,这样将来就不会有人像我一样发疯了。

account_email = '@developer.gserviceaccount.com' 
key_file = 'privatekey.p12' 
key_secret = 'notasecret' 
client = Google::APIClient.new()

key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)

 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 => account_email,
   :signing_key => key)

# # Request a token for our service account
 client.authorization.fetch_access_token!
 service = client.discovered_api('calendar', 'v3')

执行某些操作的示例应以类似的方式进行。

  @event = {
    'summary' => '',
    'location' => 'this is where the location goes',
     'description' => 'desc',
    'start' => {
      'dateTime' => '2013-02-08T10:00:00.000-07:00' # Date with :- offset so (yyyy-mm-dd T hh:mm:ss.000-offset)
    },
    'end' => {
      'dateTime' => '2013-02-08T10:25:00.000-07:00' # Date with :- offset so (yyyy-mm-dd T hh:mm:ss.000-offset)
    }
  }

  # Create event using the json structure 
  result = client.execute(:api_method => service.events.insert,
                          :parameters => {'calendarId' => '**'},
                          :body => JSON.dump(@event),
                          :headers => {'Content-Type' => 'application/json'})

关于ruby-on-rails - Google 日历服务帐户 API 导致授权无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14772616/

相关文章:

ruby-on-rails - 我是否需要在 Capistrano 上配置我的数据库服务器以及我的database.yml

ruby-on-rails - Capistrano V3 在 database.yml 上失​​败

android - 使用ViewModel和LiveData改造执行API多次

java - Google People API - 标准例如有编译错误

ruby-on-rails - Rails 4 不更新嵌套属性

ruby-on-rails - 查看路径的类名

java - 在不同语言支持下将文本转换为语音的一种巧妙方法

jquery - 安全休息 API

python - Python 的 gspread 中特定 Google 表格电子表格的持续性 SpreadsheetNotFound 错误

api - Google Places 自动完成 API 不返回邮政编码