【问题标题】:Google API Client secrets failing to load issued_atGoogle API 客户端机密无法加载发出的_at
【发布时间】:2014-09-01 07:32:36
【问题描述】:

当我尝试与 Google 建立 OAuth2 连接时收到以下错误消息。

.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/google-api-client-0.7.1/lib/google/api_client/auth/file_storage.rb:49:in `at': can't convert nil into an exact number (TypeError)

查看source,它正在尝试读取缓存的凭据文件,并且无法解析名为issued_at 的属性。

我最初在 Google 开发者控制台中使用错误的端口设置了我的应用程序。现在我更新了 client_secrets.json 但我不断收到这个错误。

我的代码正在尝试从 google 站点执行 calendar example,但已转换为使用管理目录 API,但它并没有超出身份验证步骤。

这个缓存值是从哪里来的?

require 'rubygems'
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/file_storage'
require 'sinatra'
require 'logger'

enable :sessions

CREDENTIAL_STORE_FILE = "client_secrets.json"

def logger; settings.logger end

def api_client; settings.api_client; end

def directory_api; settings.directory; end

def user_credentials
  # Build a per-request oauth credential based on token stored in session
  # which allows us to use a shared API client.
  @authorization ||= (
    auth = api_client.authorization.dup
    auth.redirect_uri = to('/oauth2callback')
    auth.update_token!(session)
    auth
  )
end

configure do
  log_file = File.open('directory.log', 'a+')
  log_file.sync = true
  logger = Logger.new(log_file)
  logger.level = Logger::DEBUG

  client = Google::APIClient.new(
    :application_name => 'Ruby Directory sample',
    :application_version => '1.0.0')

  puts "store file : #{CREDENTIAL_STORE_FILE}"

  file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
  if file_storage.authorization.nil?
    client_secrets = Google::APIClient::ClientSecrets.load
    client.authorization = client_secrets.to_authorization
    client.authorization.scope = 'https://www.googleapis.com/auth/admin.directory.user'
  else
    client.authorization = file_storage.authorization
  end

  # Since we're saving the API definition to the settings, we're only retrieving
  # it once (on server start) and saving it between requests.
  # If this is still an issue, you could serialize the object and load it on
  # subsequent runs.
  directory = client.discovered_api('admin', "directory_v1")

  set :logger, logger
  set :api_client, client
  set :directory, directory
end

before do
  # Ensure user has authorized the app
  unless user_credentials.access_token || request.path_info =~ /\A\/oauth2/
    redirect to('/oauth2authorize')
  end
end

after do
  # Serialize the access/refresh token to the session and credential store.
  session[:access_token] = user_credentials.access_token
  session[:refresh_token] = user_credentials.refresh_token
  session[:expires_in] = user_credentials.expires_in
  session[:issued_at] = user_credentials.issued_at

  file_storage = Google::APIClient::FileStorage.new(CREDENTIAL_STORE_FILE)
  file_storage.write_credentials(user_credentials)
end

get '/oauth2authorize' do
  # Request authorization
  redirect user_credentials.authorization_uri.to_s, 303
end

get '/oauth2callback' do
  # Exchange token
  user_credentials.code = params[:code] if params[:code]
  user_credentials.fetch_access_token!
  redirect to('/')
end

get '/' do

    result = api_client.execute(:api_method => directory.users.list)

  # # Fetch list of events on the user's default calandar
  # result = api_client.execute(:api_method => calendar_api.events.list,
  #                             :parameters => {'calendarId' => 'primary'},
  #                             :authorization => user_credentials)
  [result.status, {'Content-Type' => 'application/json'}, result.data.to_json]
end

【问题讨论】:

    标签: ruby google-api google-api-ruby-client


    【解决方案1】:

    将该行更改为CREDENTIAL_STORE_FILE = "#{$0}-oauth2.json",然后根据约定将您从Google 仪表板下载的json 重命名为client_secrets.jsonCREDENTIAL_STORE_FILE 是您的 OAuth 令牌的存储位置,由 FileStorage 实例创建。

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 2015-02-05
      • 1970-01-01
      • 2015-03-16
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多