Google Oauth授权失败

时间:2015-06-16 23:02:04

标签: ruby-on-rails google-drive-api google-oauth google-oauth2 google-api-ruby-client

我正在尝试在rails应用中使用'google-api-ruby-client'宝石,以便能够请求用户访问其Google云端硬盘。我成功导航到用户单击“接受”的请求权限页面。我在控制器中有一个回调函数,它捕获用户单击“接受”时返回的代码。然后我尝试获取访问令牌。这是我被挂断的地方。我收到了这个错误:

enter image description here

我当前的控制器代码如下所示:

require 'google/api_client'

class GoogleDriveController < ApplicationController

  def initialize
    @client = Google::APIClient.new(:application_name => '????',    :application_version => '1.0.0')
  end

  def auth
    @client.authorization.client_id = ENV["GOOGLE_CLIENT_ID"]
    @client.authorization.client_secret = ENV["GOOGLE_CLIENT_SECRET"]
    @client.authorization.redirect_uri = ENV["GOOGLE_REDIRECT_URI"]
    @client.authorization.scope =     'https://www.googleapis.com/auth/drive'
    authorize_url = @client.authorization.authorization_uri
    puts "https://#{authorize_url.host}#{authorize_url.path}?#{authorize_url.query}"
    render json: {google_auth_url: "https://#{authorize_url.host}#{authorize_url.path}?#{authorize_url.query}"}
  end

  def callback
    @client.authorization.code = params[:code] if params[:code]
    access_token = @client.authorization.fetch_access_token!
    @drive = @client.discovered_api('drive', 'v2')
  end

end

非常感谢任何正确方向的帮助或推动。

谢谢!